Programming Articles

Page 356 of 2547

Python – Remove Dictionaries with Matching Values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 328 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 304 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 192 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 287 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 497 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 379 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 412 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 320 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

Python – Filter immutable rows representing Dictionary Keys from Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 224 Views

When working with matrices (lists of lists) in Python, you may need to filter rows containing only immutable data types that can serve as dictionary keys. This involves checking if all elements in each row are immutable types like int, str, tuple, bool, or float. Understanding Immutable Types In Python, immutable types can be used as dictionary keys because they are hashable. These include: int − Integer numbers str − String values tuple − Ordered collections (if containing only immutable elements) bool − Boolean values float − Floating-point numbers Mutable types like list, dict, ...

Read More
Showing 3551–3560 of 25,466 articles
« Prev 1 354 355 356 357 358 2547 Next »
Advertisements