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
Python Articles
Page 811 of 855
Get first element with maximum value in list of tuples in Python
When working with a list of tuples, you may need to find the first element of the tuple that has the maximum value in a specific position. This is useful when you have data like scores, dates, or measurements stored as tuples and want to identify the tuple with the highest value. Using itemgetter() and max() The itemgetter() function from the operator module extracts values from a specific index position. Combined with max(), it finds the tuple with the maximum value ? from operator import itemgetter # initializing list data = [('Mon', 3), ('Tue', 20), ...
Read MoreFlatten given list of dictionaries in Python
We have a list whose elements are dictionaries. We need to flatten it to get a single dictionary where all these list elements are present as key-value pairs. Using for Loop and update() We take an empty dictionary and add elements to it by reading the elements from the list. The addition of elements is done using the update() function ? listA = [{'Mon':2}, {'Tue':11}, {'Wed':3}] # printing given arrays print("Given array:", listA) print("Type of Object:", type(listA)) res = {} for x in listA: res.update(x) # Result print("Flattened object:", ...
Read MoreFirst occurrence of True number in Python
In this article, we will find the first occurrence of a non-zero (True) number in a given list. In Python, zero evaluates to False while non-zero numbers evaluate to True, making this a common programming task. Using enumerate() with next() The enumerate() function provides both index and value, while next() returns the first matching element ? Example numbers = [0, 0, 13, 4, 17] # Given list print("Given list:", numbers) # Using enumerate to get index of first non-zero number result = next((i for i, j in enumerate(numbers) if j), None) # ...
Read MoreFirst Non-Empty String in list in Python
Given a list of strings, we need to find the first non-empty element. The challenge is that there may be one, two, or many empty strings at the beginning of the list, and we need to dynamically find the first non-empty string. Using next() The next() function moves to the next element that satisfies a condition. We can use it with a generator expression to find the first non-empty string ? Example string_list = ['', 'top', 'pot', 'hot', ' ', 'shot'] # Given list print("Given list:", string_list) # Using next() with generator expression ...
Read MoreFinding relative order of elements in list in Python
We are given a list whose elements are integers. We are required to find the relative order, which means finding the rank (position) each element would have if the list were sorted in ascending order. Using sorted() and index() We first sort the entire list and then find the index of each element in the sorted list ? Example numbers = [78, 14, 0, 11] # printing original list print("Given list is:") print(numbers) # using sorted() and index() result = [sorted(numbers).index(i) for i in numbers] # printing result print("List with relative ordering ...
Read MoreFind whether all tuple have same length in Python
In this article, we will learn different methods to check if all tuples in a given list have the same length. This is useful when validating data structures or ensuring consistency in tuple collections. Using Manual Iteration with len() We can iterate through each tuple and compare its length to a reference value. If any tuple has a different length, we know they are not all the same ? schedule = [('Mon', '2 pm', 'Physics'), ('Tue', '11 am', 'Maths')] # Print the original list print("Given list of tuples:") print(schedule) # Check if all tuples ...
Read MoreFind 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 More