Articles on Trending Technologies

Technical articles with clear explanations and examples

Python program to Sort a List of Dictionaries by the Sum of their Values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 308 Views

When working with lists of dictionaries, you may need to sort them based on the sum of their values. Python provides multiple approaches to achieve this using the sort() method with custom key functions. Method 1: Using a Custom Function Define a function to calculate the sum of dictionary values and use it as the sorting key − def sum_value(row): return sum(list(row.values())) my_dict = [{21: 13, 44: 35, 34: 56}, {11: 75, 70: 19, 39: 70}, {1: 155}, {48: 29, 17: 53}] print("The dictionary is:") print(my_dict) my_dict.sort(key=sum_value) print("The ...

Read More

Python – Remove Dictionaries with Matching Values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 316 Views

When working with lists of dictionaries, you may need to remove dictionaries that have matching values for a specific key. Python provides several approaches using dictionary comprehensions and set operations. Basic Example Here's how to remove dictionaries from one list that have matching values with dictionaries in another list ? # List of dictionaries to filter dict_list_1 = [ {'Hi': 32, "there": 32, "Will": 19}, {'Hi': 19, "there": 100, "Will": 13}, {'Hi': 72, "there": 19, "Will": 72} ] print("The first dictionary list ...

Read More

Python – Find the frequency of numbers greater than each element in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 281 Views

When it is required to find the frequency of numbers greater than each element in a list, a list comprehension and the 'sum' method can be used to count elements efficiently. Example The following example demonstrates how to count elements greater than each position in the list ? my_list = [24, 13, 72, 22, 12, 47] print("The list is :") print(my_list) my_result = [sum(1 for element in my_list if element > index) for index in my_list] print("The result is :") print(my_result) Output The list is : [24, 13, 72, ...

Read More

Python Program to test whether the length of rows are in increasing order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 172 Views

When it is required to test whether the length of rows are in increasing order, a simple iteration and a Boolean value is used. This technique helps verify that each nested list has more elements than the previous one. Example my_list = [[55], [12, 17], [25, 32, 24], [58, 36, 57, 19, 14]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list) - 1): if len(my_list[index + 1])

Read More

Python program to find the sum of all even and odd digits of an integer list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the modulus operator are used. Below is a demonstration of the same − Example my_list = [369, 793, 2848, 4314, 57467] print("The list is :") print(my_list) sum_odd = 0 sum_even = 0 for index in my_list: for element in str(index): if int(element) % 2 == 0: ...

Read More

Python – Test for Word construction from character list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 263 Views

When it is required to test if a word can be constructed from a character list, the all() function and the count() method are used together. This approach checks if each character in the target word appears enough times in the available character list. Syntax all(word.count(char)

Read More

Python – Get Every Element from a String List except for a specified letter

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 465 Views

When working with string lists in Python, you might need to extract or filter out specific characters. This article demonstrates how to get every element from a string list except for a specified letter using list comprehension. Example Here's how to remove a specific character from all strings in a list − my_list = ["hi", "is", "great", "pyn", "pyt"] print("The list is:") print(my_list) my_key = 'n' print("The letter to exclude is:") print(my_key) my_result = [] for sub in my_list: my_result.append(''.join([element for element in sub if element != ...

Read More

Python - Print rows from the matrix that have same element at a given index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 363 Views

When working with matrices (lists of lists), you may need to filter rows where all elements have the same digit at a specific position. This can be achieved using list comprehension with the all() function to check if elements share a common digit at a given index position. Problem Statement Given a matrix and an index position, find all rows where every element has the same digit at that specific index position when converted to string. Example Let's find rows where all elements have the same digit at index position 1 ? matrix = ...

Read More

Python Program – Print the count of either peaks or valleys from a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 386 Views

When it is required to count peaks (values higher than both neighbors) or valleys (values lower than both neighbors) from a list, we use iteration with specific conditions to compare each element with its adjacent elements. Understanding Peaks and Valleys A peak is an element that is greater than both its left and right neighbors. A valley is an element that is smaller than both its left and right neighbors. We only check elements that have both left and right neighbors (excluding first and last elements). Peaks and Valleys in a List ...

Read More

Python – Consecutive Division in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 297 Views

When it is required to find the consecutive division in a list, a method is defined that iterates over the elements of the list and uses the '/' operator to determine the result. This operation divides the first element by all subsequent elements sequentially. Below is a demonstration of the same ? Example def consec_division(my_list): my_result = my_list[0] for idx in range(1, len(my_list)): my_result /= my_list[idx] return my_result my_list = [2200, 500, 100, 50, 20, ...

Read More
Showing 3951–3960 of 61,297 articles
« Prev 1 394 395 396 397 398 6130 Next »
Advertisements