Python – Sort Dictionaries by Size

AmitDiwan
Updated on 26-Mar-2026 01:19:24

281 Views

When working with Python dictionaries, you often need to sort them by their size (number of key-value pairs). Python provides several approaches to accomplish this using the len() function as a sorting key. Using a Custom Function with sort() The most straightforward approach is to define a helper function that returns the length of each dictionary ? def get_len(element): return len(element) dict_list = [ {24: 56, 29: 11, 10: 22, 42: 28}, {54: 73, 59: 11}, {13: ... Read More

Python – Cross Pattern Pairs in List

AmitDiwan
Updated on 26-Mar-2026 01:19:05

297 Views

When it is required to display cross pattern pairs in list, a list comprehension and the multiplication operator are used to create all possible products between elements of two lists. What is Cross Pattern Pairs? Cross pattern pairs refer to multiplying each element from the first list with every element from the second list, creating a Cartesian product of multiplications. Example my_list_1 = [14, 35, 26] my_list_2 = [36, 24, 12] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) result = [i * j for j in my_list_1 for ... Read More

Python – Nearest occurrence between two elements in a List

AmitDiwan
Updated on 26-Mar-2026 01:18:52

367 Views

When you need to find the nearest occurrence between two elements in a list, you can create a function that calculates the minimum distance between all occurrences of the first element and the target element. This approach uses list comprehension and the abs() function to find the closest match. Example Here's how to find the index of the nearest occurrence ? def nearest_occurrence_list(my_list, x, y): if x not in my_list or y not in my_list: return -1 ... Read More

Python – Filter Tuples with Integers

AmitDiwan
Updated on 26-Mar-2026 01:18:34

263 Views

When it is required to filter tuples that contain only integers, a simple iteration with the 'not' operator and the 'isinstance' method can be used to check each element's data type. Example Below is a demonstration of filtering tuples with integers − my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): ... Read More

Python – Extract tuples with elements in Range

AmitDiwan
Updated on 26-Mar-2026 01:18:20

361 Views

When working with tuples in Python, you may need to extract only those tuples where all elements fall within a specific range. This can be accomplished using the filter() function combined with lambda expressions and the all() function. Syntax The general approach uses this pattern ? filtered_tuples = list(filter(lambda tuple: all(start

Python – Extract Kth element of every Nth tuple in List

AmitDiwan
Updated on 26-Mar-2026 01:18:03

407 Views

When working with lists of tuples, you may need to extract the Kth element from every Nth tuple. This can be accomplished using simple iteration with the range() function and list indexing. Understanding the Problem Given a list of tuples, we want to: Select every Nth tuple (e.g., every 3rd tuple: indices 0, 3, 6...) From each selected tuple, extract the Kth element Collect these elements into a new list Example Below is a demonstration of extracting the 1st element (K=1) from every 3rd tuple (N=3) − tuples_list = [(54, 51, ... Read More

Python – Ordered tuples extraction

AmitDiwan
Updated on 26-Mar-2026 01:17:45

256 Views

When working with tuples, you might need to extract only those tuples that are already in sorted order. Python provides an elegant solution using list comprehension combined with the sorted() function and tuple comparison. What are Ordered Tuples? An ordered tuple is a tuple where elements are arranged in ascending order. For example, (13, 24, 56) is ordered, while (15, 74, 36, 22, 54) is not. Extracting Ordered Tuples The key idea is to compare each tuple with its sorted version. If they are equal, the tuple is already in order ? my_list = ... Read More

Python – Concatenate Rear elements in Tuple List

AmitDiwan
Updated on 26-Mar-2026 01:17:28

294 Views

When working with tuples in Python, you might need to concatenate the last (rear) elements from each tuple in a list. This can be accomplished using list comprehension with the join() method to create a single concatenated string. Example Below is a demonstration of concatenating rear elements from a tuple list − my_tuple = [(13, 42, "Will"), (48, "is a"), ("good boy", )] print("The tuple is : ") print(my_tuple) my_result = " ".join([sub[-1] for sub in my_tuple]) print("The result is : ") print(my_result) Output The tuple is : ... Read More

Python – Find Kth Even Element

AmitDiwan
Updated on 26-Mar-2026 01:17:13

233 Views

Finding the Kth even element in a list is a common programming task. Python provides several approaches using list comprehension, filtering, and indexing techniques. Method 1: Using List Comprehension Create a list of even elements and access the Kth element directly ? numbers = [14, 63, 28, 32, 18, 99, 13, 61] print("The list is:") print(numbers) K = 3 print("The value of K is:") print(K) # Find Kth even element (1-indexed) result = [element for element in numbers if element % 2 == 0][K-1] print("The Kth even element is:") print(result) ... Read More

Python – Split List on next larger value

AmitDiwan
Updated on 26-Mar-2026 01:16:57

241 Views

When it is required to split a list based on the next larger value, a list comprehension, the iter method and the islice methods are used. This technique allows you to divide a list into chunks of specified sizes sequentially. Basic Example Below is a demonstration of splitting a list into chunks of different sizes − from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87, 11] print("The list is :") print(my_list) length_to_split = [2, 5, 3] print("The split length list is :") print(length_to_split) temp = iter(my_list) my_result = [list(islice(temp, ... Read More

Advertisements