AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 119 of 840

Python – Negative index of Element in List

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

Getting the negative index of an element in a list helps you find the position from the end. Python allows negative indexing where -1 refers to the last element, -2 to the second last, and so on. Understanding Negative Indexing In Python, negative indices count backward from the end of the list: 52 47 18 22 ...

Read More

Python – Custom Lower bound a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 547 Views

When working with numerical data, you may need to set a custom lower bound for a list. This means replacing any values below a threshold with the threshold value itself. Python's list comprehension provides an elegant solution for this task. Syntax result = [element if element >= threshold else threshold for element in original_list] Example Let's apply a lower bound of 50 to a list of integers ? numbers = [51, 71, 86, 21, 11, 35, 67] print("Original list:") print(numbers) threshold = 50 print(f"Lower bound threshold: {threshold}") result = ...

Read More

Python – Remove Elements in K distance with N

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 230 Views

When it is required to remove elements which are at K distance with N, a list comprehension along with a specific condition is used. This means removing elements that fall within the range [N-K, N+K]. Understanding K Distance An element is considered at K distance from N if the absolute difference between the element and N is less than or equal to K. In other words, elements in the range [N-K, N+K] are at K distance from N. Elements at K Distance from N ...

Read More

Python – Disjoint Strings across Lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 291 Views

When we need to find pairs of strings from two lists that share no common characters, we use disjoint string analysis. Two strings are disjoint if they have no characters in common. Understanding Disjoint Strings Disjoint strings are strings that don't share any common characters. For example, "fun" and "its" are disjoint because they have no letters in common. Example Here's how to find all disjoint string pairs from two lists ? from functools import reduce def determine_disjoint_pairs(disjoint_data, my_result=[]): if not disjoint_data and not reduce(lambda a, b: set(a) & ...

Read More

Python – Check if particular value is present corresponding to K key

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 248 Views

When working with a list of dictionaries in Python, you often need to check if a particular value exists for a specific key across all dictionaries. This can be efficiently done using list comprehension with the in operator. Example Here's how to check if a value is present corresponding to a specific key ? my_list = [ {"python": "14", "is": "great", "fun": "1"}, {"python": "cool", "is": "fun", "best": "81"}, {"python": "93", "is": "CS", "amazing": "16"} ] print("The list is:") print(my_list) K = ...

Read More

Python – Sort by Uppercase Frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 372 Views

Sorting strings by uppercase frequency means arranging them based on the count of capital letters they contain. This is useful for organizing text data by capitalization patterns. Python's sort() method with a custom key function makes this straightforward. How It Works The approach uses a custom function that counts uppercase letters in each string, then sorts the list using this count as the sorting key ? def count_uppercase(text): return len([char for char in text if char.isupper()]) words = ["pyt", "is", "FUN", "to", "Learn"] print("Original list:") print(words) words.sort(key=count_uppercase) print("Sorted ...

Read More

Python program to extract only the numbers from a list which have some specific digits

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 804 Views

When you need to extract numbers from a list that contain only specific digits, you can use list comprehension with the all() function. This technique filters numbers by checking if every digit in each number exists in your allowed digits set. Syntax result = [num for num in numbers if all(int(digit) in allowed_digits for digit in str(num))] Example Let's extract numbers that contain only the digits 2, 3, 4, and 5 ? numbers = [3345, 2345, 1698, 2475, 1932] print("The original list is:") print(numbers) allowed_digits = [2, 3, 4, 5] ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 313 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 286 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
Showing 1181–1190 of 8,392 articles
« Prev 1 117 118 119 120 121 840 Next »
Advertisements