Python Articles

Page 135 of 852

Element repetition in list in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 555 Views

There are scenarios when we need to repeat the values in a list. This duplication of values can be achived in python in the following ways.Using nested for loopIt is a straight forward approach in which pick each element, take through a inner for loop to create its duplicate and then pass both of them to an outer for loop.Example# Given list listA = ['Mon', 'Tue', 9, 3, 3] print("Given list : ", listA) # Adding another element for each element Newlist = [i for i in listA for n in (0, 1)] # Result print("New list ...

Read More

Element with largest frequency in list in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 584 Views

A lot of statistical data analysis tries to find the values which have maximum frequency in a given list of values. Python provides multiple approaches using which we can find such value form a given list. Below are the approaches.Using CounterThe Counter function from collections module has a options which can directly find the most common element in a given list. We have the most_common function to which we pass a parameter 1 for only one element with highest frequency and pass 2 if we need two elements which have highest frequency.Examplefrom collections import Counter # Given list listA ...

Read More

Equate two list index elements in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 620 Views

During data manipulation with Python , we may need to bring two lists together and equate the elements in each of them pair wise. Which means the element at index 0 from list 1 will be equated with element from index 0 of list2 and so on.With tupleThe tuple function will be leveraged to take elements form each list in sequence and matching them up. We first store the result in a temp string which has the pattern in which the output of matching up of the values form lists will be displayed.ExamplelistA = ['day1', 'day2', 'day3'] listB = ['Mon', ...

Read More

Extract numbers from list of strings in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

While using python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers form letters which can be used for future calculations.With splitThe split functions splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and text. We will use that along with a for loop to capture eachExamplelistA = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given list ...

Read More

Extract only characters from given string in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 3K+ Views

A piece of data may contain letters, numbers as well as special characters. If we are interested in extracting only the letters form this string of data, then we can use various options available in python.With isalphaThe isalpha function will check if the given character is an alphabet or not. We will use this inside a for loop which will fetch each character from the given string and check if it is an alphabet. The join method will capture only the valid characters into the result.ExamplestringA = "Qwer34^&t%y" # Given string print("Given string : ", stringA) # Find ...

Read More

Find frequency of given character at every position in list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 309 Views

Lets consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.Consider a list of lists given below.listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]In the abobe list we have elements which are lists with 3 elements. If I consider the first inner list tit has a, a, b at positions 0, 1, 2. Similarly for the 3rd list it is c, a, b at 0, ...

Read More

Find sum of frequency of given elements in the list in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 552 Views

A given list has many repeated items. We are interested in finding out the sum of the frequency of some such items which are repeated in the list. Below are the approaches how we can achieve this.With sumWe have two lists. One has the list of values and other has the values whose frequency needs to be checked from the first list. So we create a for loop to count the number of occurrences of the elements from the second list in the first list and then apply the sum function to get the final sum of frequency.Examplechk_list= ['Mon', 'Tue'] ...

Read More

Finding frequency in list of tuples in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 579 Views

Many different types of data container can get mixed up in python. A list can have elements each of which is a tuple. In this article we will take such a list and find the frequency of element in the tuples which are themselves elements of a list.Using count and mapWe apply a lambda function to count through each of the first element in the tuples present in the list. Then apply a map function to arrive at the total count of the element we are searching for.Example# initializing list of tuples listA = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed')] ...

Read More

Get positive elements from given list of lists in Python

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 547 Views

Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out only the positive numbers from a list of lists. In the result a new list will contain nested lists containing positive numbers.With for inHere we simply apply the mathematical operator to check for the value of elements in a list using a for loop. If the value is positive we capture it as a list and Outer for loop stores as a final list of lists.ExamplelistA = [[-9, -1, 3], [11, -8, -4, 434, 0]] # ...

Read More

Assign multiple variables with a Python list values

Pradeep Elance
Pradeep Elance
Updated on 11-Mar-2026 2K+ Views

Depending on the need of the program we may an requirement of assigning the values in a list to many variables at once. So that they can be further used for calculations in the rest of the part of the program. In this article we will explore various approaches to achieve this.Using for inThe for loop can help us iterate through the elements of the given list while assigning them to the variables declared in a given sequence.We have to mention the index position of values which will get assigned to the variables.ExamplelistA = ['Mon', ' 2pm', 1.5, '11 miles'] ...

Read More
Showing 1341–1350 of 8,519 articles
« Prev 1 133 134 135 136 137 852 Next »
Advertisements