Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 14 of 2110
Find top K frequent elements from a list of tuples in Python
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 MoreFind the tuples containing the given element from a list of tuples in Python
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 MoreFind the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
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 MoreFind the list elements starting with specific letter in Python
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 MoreFind most frequent element in a list in Python
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 MoreFind maximum value in each sublist in Python
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 MoreCount tuples occurrence in list of tuples in Python
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 MoreCount the sublists containing given element in a list in Python
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 MoreCount the Number of matching characters in a pair of string in Python
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 MoreCount of elements matching particular condition in Python
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