Found 10784 Articles for Python

Python program to concatenate every elements across lists

Vikram Chiluka
Updated on 27-Jan-2023 11:38:40

471 Views

In this article, we will learn a Python program to concatenate every element across lists. Methods Used The following are the various methods to accomplish this task − Using the List Comprehension and String Concatenation Using itertools.product() Function Example Assume we have taken two input lists. We will now concatenate every element across the given two lists Input inputList1 = ["hello", "tutorialspoint", "python"] inputList2 = ["coding", "articles"] Output Resultant paired combinations list: ['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles'] Method 1: Using the List Comprehension and String Concatenation List Comprehension When ... Read More

Python Program to Capitalize repeated characters in a string

Vikram Chiluka
Updated on 27-Jan-2023 11:37:38

241 Views

In this article, we will learn how to capitalize repeated characters in a string in python. Methods Used The following are the various methods to accomplish this task - Using Dictionary Hashing Using count() Function Using replace() and len() functions Using Counter() function Example Assume we have taken an input string containing some random text. We will now convert the repeated characters in an input string into uppercase using the above methods. Input inputString = 'hello tutorialspoint' Output heLLO TuTOrIaLspOInT In the above input string, the characters l, o, t, i are repeated. Hence they are ... Read More

Python Program to Alternate list elements as key-value pairs

Vikram Chiluka
Updated on 27-Jan-2023 11:31:42

441 Views

In this article, we will learn how to get alternate list elements as key-value pairs in python. Assume we have taken an input list containing integers. We will now Methods Used The following are the various methods used to accomplish this task − Using for loop Using dictionary comprehension and list slicing Example Assume we have taken an input list. We will now print alternate list elements as key-value pairs. Input inputList = [20, 13, 5, 2, 6, 8, 18, 3] Output Dictionary with alternate list elements as key-value pairs: {13: 2, 2: 8, 8: 3, 20: ... Read More

How to Common keys in list and dictionary using Python

Vikram Chiluka
Updated on 27-Jan-2023 11:30:17

681 Views

In this article, we will learn how to find common keys in a list and dictionary in python. Methods Used The following are the various methods to accomplish this task − Using the ‘in’ operator and List Comprehension Using set(), intersection() functions Using keys() function & in operator Using the Counter() function Example Assume we have taken an input dictionary and list. We will find the common elements in the input list and keys of a dictionary using the above methods. Input inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10} inputList = ["hello", "tutorialspoint", ... Read More

Find the difference of the sum of list elements that are missing from Matrix and vice versa in python

Vikram Chiluka
Updated on 27-Jan-2023 11:26:23

125 Views

In this article, we will learn how to find the difference of the sum of list elements that are missing from Matrix and vice versa. Methods Used The following are the various methods to accomplish this task − Using for loop & from_iterable() function Using sum() & from_iterable() functions Using the Counter() function Example Assume we have taken an input matrix and a target list. We will now find the difference of the sum of list elements that are missing from Matrix and vice versa. Input inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, ... Read More

Case-insensitive string replacement using Python Program

Vikram Chiluka
Updated on 27-Jan-2023 11:20:58

4K+ Views

In this article, we will learn Case insensitive string replacement in python. Methods Used The following are the various methods to accomplish this task − Using re.IGNORECASE, re.escape(), re.sub() Using re.sub(), lambda, re.escape() functions Using split(), lower() & replace() functions Using split(), list() and join() functions Method 1: Using re.IGNORECASE, re.escape(), re.sub() re.compile() function A regular expression pattern can be combined with pattern objects, which can then be used for pattern matching. This function also allows searching for a pattern again without rewriting it. Syntax re.compile(pattern, repl, string) re.sub() function The string with replaced ... Read More

Interchange elements of the first and last rows in the matrix using Python

Vikram Chiluka
Updated on 27-Jan-2023 13:43:56

950 Views

In this article, we will learn a python program to interchange elements of the first and last rows in a matrix. Methos Used Using Temporary Variable(Using Nested Loops) Using Swapping(“, ”) Operator(Without Using Any Loops) Using pop(), insert() & append() functions Method 1: Using Temporary Variable(Using Nested Loops) Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a function swapFirstandLastRow() to swap the first and last rows of an input matrix by accepting the input matrix, rows, and columns as arguments. Traverse through the matrix rows and columns and Swap ... Read More

Interchange Diagonals of Matrix using Python

Vikram Chiluka
Updated on 27-Jan-2023 13:40:13

479 Views

In this article, we will learn a python program to interchange the diagonals of the matrix. Assume we have taken an NxN input matrix. We will now interchange the diagonals of an input matrix using the below methods. Methos Used The following are the various methods to accomplish this task − Using the Nested For Loops with Temporary Variable Using ‘, ’ Swapping Operator Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a variable to store the input number of rows of a matrix Create a function printGivenMatrix() that ... Read More

How to remove the last element from a set in Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:38:08

8K+ Views

In this article, we will learn how to remove the last element from the set in Python. Methods Used The following are the various methods to accomplish this task − Using the discard() function Using the remove() function Using the pop() function Using the list slicing Method 1: Using the discard() function The discard() function removes the element from the set, bypassing the element as an argument to it. Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a variable to store the input set containing integers. Use the discard() ... Read More

How to perform accurate Decimal Calculations using Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:36:36

1K+ Views

In this article, we will learn how to perform Accurate Decimal Calculations in Python. Methods Used Using the Decimal() function of the decimal Module Using the fsum() function of the math module The inability of floating-point numbers to accurately represent all base-10 numbers is a well-known drawback. Furthermore, even straightforward mathematical computations have few errors. For instance − Example The following program shows the inability of floating-point integers to express all base-10 numbers accurately − x = 4.2 y = 3.1 # printing the sum of both the variables print("x + y =", x + ... Read More

Advertisements