Server Side Programming Articles

Page 14 of 2110

Find top K frequent elements from a list of tuples in Python

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

We have a list of tuples and need to find the top K elements with highest values. For example, if K is 3, we need to find the three tuples with the largest second values. Using defaultdict and sorted This approach uses defaultdict to group elements and then sorts them by value to get the top K elements. Example import collections from operator import itemgetter from itertools import chain # Input list initialization listA = [[('Mon', 126)], [('Tue', 768)], [('Wed', 512)], [('Thu', 13)], [('Fri', 341)]] # Set K K = 3 # ...

Read More

Find the tuples containing the given element from a list of tuples in Python

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

A list can have tuples as its elements. In this article we will learn how to identify those tuples which contain a specific search element from a list of tuples using different methods. Using List Comprehension with Condition We can use list comprehension with a conditional statement to filter tuples that contain the target element ? listA = [('Mon', 3), ('Tue', 1), ('Mon', 2), ('Wed', 3)] test_elem = 'Mon' # Given list print("Given list:") print(listA) print("Check value:") print(test_elem) # Using list comprehension with condition res = [item for item in listA if item[0] == ...

Read More

Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python

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

In this article, we need to find a number from a list that occurs an odd number of times. We'll use Python's lambda function with the reduce() function to solve this problem efficiently. The key insight is using the XOR (^) operation, which has a special property: when you XOR a number with itself, the result is 0. This means numbers appearing an even number of times will cancel out, leaving only the number that appears an odd number of times. How XOR Works for This Problem Let's understand the XOR logic with a simple example: ...

Read More

Find the list elements starting with specific letter in Python

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

In this article, we will explore different methods to find all elements from a list that start with a specific letter. Python provides several approaches to achieve this efficiently. Using Index and lower() This method uses the lower() function to make the comparison case-insensitive. We access the first character of each element using index [0] and compare it with the test letter ? days = ['Mon', 'Tue', 'Wed', 'Thu'] test_letter = 'T' print("Given list:") print(days) # Using lower() and index to find elements starting with specific letter result = [item for item in days ...

Read More

Find most frequent element in a list in Python

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

In this article, we will see how to find the element which is most common in a given list. In other words, the element with the highest frequency. Python provides several approaches to solve this problem efficiently. Using max() and count() We apply the set() function to get unique elements of the list, then use count() to find the frequency of each element. Finally, we apply max() with a key function to get the element with the highest frequency. Example # Given list numbers = [45, 20, 11, 50, 17, 45, 50, 13, 45] print("Given ...

Read More

Find maximum value in each sublist in Python

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

We are given a list of lists (nested lists). Our task is to find the maximum value in each sublist and return them as a new list. Python provides several approaches to accomplish this efficiently. Using List Comprehension with max() The most Pythonic approach uses list comprehension combined with the max() function to iterate through each sublist ? data = [[10, 13, 454, 66, 44], [10, 8, 7, 23], [5, 15, 25]] # Given list print("The given list:") print(data) # Use max with list comprehension result = [max(sublist) for sublist in data] print("Maximum ...

Read More

Count tuples occurrence in list of tuples in Python

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

A list is made up of tuples as its elements. In this article we will count the number of unique tuples present in the list and their occurrences using different approaches. Using defaultdict We treat the given list as a defaultdict data container and count the elements in it using iteration. The defaultdict automatically initializes missing keys with a default value. Example import collections tuple_list = [('Mon', 'Wed'), ('Mon', ), ('Tue', ), ('Mon', 'Wed')] # Given list print("Given list:", tuple_list) res = collections.defaultdict(int) for elem in tuple_list: res[elem] ...

Read More

Count the sublists containing given element in a list in Python

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

When working with lists in Python, you often need to count how many times a specific element appears. This article demonstrates three different approaches to count occurrences of a given element in a list. Using range() and len() This method uses a loop with range() and len() to iterate through the list indices. A counter variable tracks how many times the element is found ? days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] target = 'Mon' print("Given list:", days) print("Element to check:", target) count = 0 for i in range(len(days)): if ...

Read More

Count the Number of matching characters in a pair of string in Python

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

We are given two strings and need to find the count of characters in the first string that are also present in the second string. Python provides several approaches to solve this problem efficiently. Using Set Intersection The set function gives us unique values of all elements in a string. We use the & operator to find common elements between the two strings ? Example strA = 'Tutorials Point' uniq_strA = set(strA) print("Given String:", strA) strB = 'aeio' uniq_strB = set(strB) print("Search character strings:", strB) common_chars = uniq_strA & uniq_strB print("Count of matching ...

Read More

Count of elements matching particular condition in Python

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

In this article, we will see how to count elements in a Python list that match a particular condition. We'll explore different approaches to filter and count elements based on specific criteria. Using Generator Expression with sum() This approach uses a generator expression with an if condition inside the sum() function. The expression generates 1 for each element that matches the condition ? Example days = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:") print(days) # Count elements that are 'Mon' or 'Wed' count = sum(1 for day in days if ...

Read More
Showing 131–140 of 21,091 articles
« Prev 1 12 13 14 15 16 2110 Next »
Advertisements