Server Side Programming Articles

Page 890 of 2109

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

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 574 Views

When working with lists containing repeated elements, we often need to find the sum of frequencies for specific items. Python provides several approaches to calculate this efficiently ? Using sum() with count() This method uses the built-in count() method to find frequency of each element and sum() to calculate the total ? chk_list = ['Mon', 'Tue'] big_list = ['Mon', 'Tue', 'Wed', 'Mon', 'Mon', 'Tue'] # Apply sum res = sum(big_list.count(elem) for elem in chk_list) # Printing output print("Given list to be analysed:") print(big_list) print("Given list with values to be analysed:") print(chk_list) print("Sum of the ...

Read More

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

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 325 Views

Let's 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']] print("Original list of lists:") print(listA) Original ...

Read More

Extract only characters from given string in Python

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

Sometimes strings contain a mix of letters, numbers, and special characters. When you need to extract only the alphabetic characters from such strings, Python provides several efficient methods. Using isalpha() Method The isalpha() method checks if a character is alphabetic. You can combine it with a loop and join() to extract only letters ? Example text = "Qwer34^&t%y" # Given string print("Given string:", text) # Extract characters using isalpha() result = "" for char in text: if char.isalpha(): result = "".join([result, ...

Read More

Extract numbers from list of strings in Python

Pradeep Elance
Pradeep Elance
Updated on 15-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 from letters which can be used for future calculations. Using split() Method The split() function 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 numbers. We will use that along with list comprehension to extract each number ? days_with_numbers = ['Mon-2', ...

Read More

Equate two list index elements in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 649 Views

During data manipulation with Python, we may need to bring two lists together and equate the elements in each of them pair wise. This means the element at index 0 from list 1 will be equated with element from index 0 of list 2 and so on. Using Tuple Formatting The tuple function can be leveraged to take elements from each list in sequence and match 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 from lists will be displayed ? ...

Read More

Element with largest frequency in list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 613 Views

Finding the element with the largest frequency in a list is a common task in data analysis and statistics. Python provides several built-in approaches to accomplish this efficiently using the collections.Counter class and the statistics.mode function. Using Counter from collections The Counter class provides a most_common() method that returns elements with their frequencies in descending order. You can pass a parameter to limit the number of results ? Example from collections import Counter # Given list days_and_numbers = ['Mon', 'Tue', 'Mon', 9, 3, 3] print("Given list:", days_and_numbers) # Find the single most ...

Read More

Element repetition in list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 605 Views

There are scenarios when we need to repeat the values in a list. This duplication of values can be achieved in Python using several approaches. Let's explore different methods to duplicate each element in a list. Using List Comprehension with Nested Loop This is a straightforward approach where we use list comprehension with nested iteration to duplicate each element ? # Given list data = ['Mon', 'Tue', 9, 3, 3] print("Given list:", data) # Adding another element for each element new_list = [i for i in data for n in (0, 1)] # ...

Read More

Dividing two lists in Python

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

Dividing two lists element-wise is a common operation in Python data manipulation. You can achieve this using several approaches including zip() with list comprehension, the operator.truediv function with map(), or NumPy arrays for numerical computations. Using zip() with List Comprehension The zip() function pairs elements from two lists, allowing you to apply division to corresponding elements ? # Given lists numbers1 = [12, 4, 0, 24] numbers2 = [6, 3, 8, -3] print("Given list 1:", numbers1) print("Given list 2:", numbers2) # Use zip with list comprehension result = [i / j for i, j ...

Read More

Difference of two lists including duplicates in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 356 Views

Sometimes we need to find the differences between two lists while preserving duplicates. This operation is similar to mathematical subtraction where elements from the first list are removed based on their occurrences in the second list. If an element appears multiple times, only the matching count is removed. Using Counter from collections The Counter class from the collections module tracks element frequencies and supports subtraction operations ? from collections import Counter # initializing lists listA = ['Mon', 'Tue', 9, 3, 3] listB = ['Mon', 3] # printing original lists print("Given ListA :", listA) print("Given ...

Read More

Dictionary with index as value in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 913 Views

In this article we will learn how to create a dictionary from a Python list, where the list values become dictionary keys and their positions become the values. This is useful when you need to map elements to their index positions. Using enumerate with Dictionary Comprehension The enumerate() function adds a counter to each element in the list. We can use it with dictionary comprehension to create a dictionary where list values become keys and their positions become values ? Example days = ['Mon', 'Tue', 'Wed', 'Wed', 11, 11] # Given list print("Given list:", ...

Read More
Showing 8891–8900 of 21,090 articles
« Prev 1 888 889 890 891 892 2109 Next »
Advertisements