Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Remove Tuples with difference greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 244 Views

When working with tuples, you may need to filter out tuples where the absolute difference between elements exceeds a threshold value K. Python provides an efficient way to accomplish this using list comprehension and the abs() function. Example Here's how to remove tuples with difference greater than K ? my_tuple = [(41, 18), (21, 57), (39, 22), (23, 42), (22, 10)] print("The tuple is :") print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1])

Read More

Python – Remove dictionary from a list of dictionaries if a particular value is not present

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

When you need to remove a dictionary from a list based on whether a particular value is present or absent, you can use several approaches. The most common methods are iteration with del, list comprehension, or the filter() function. Method 1: Using del with Index-based Iteration This method iterates through the list and removes the dictionary when a condition is met ? my_list = [{"id": 1, "data": "Python"}, {"id": 2, "data": "Code"}, {"id": 3, ...

Read More

Python – Extract Strings with Successive Alphabets in Alphabetical Order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 296 Views

When working with strings, you may need to extract strings that contain successive alphabets in alphabetical order. This means finding strings where at least two consecutive characters follow each other in the alphabet (like 'hi' where 'h' and 'i' are consecutive). Understanding Successive Alphabets Successive alphabets are characters that follow each other in the alphabet sequence. For example: 'ab' - 'a' and 'b' are successive 'hi' - 'h' and 'i' are successive 'xyz' - 'x', 'y', 'z' are all successive Method: Using ord() Function The ord() function returns the Unicode code point of a ...

Read More

Python – Test for desired String Lengths

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 284 Views

When it is required to test for desired string lengths, a simple iteration and the len() method can be used. This technique helps verify that each string in a list matches its corresponding expected length. Using Basic Iteration Below is a demonstration of checking string lengths using a for loop ? strings = ["python", "is", "fun", "to", "learn", "Will", "how"] print("The list is :") print(strings) expected_lengths = [6, 2, 3, 2, 5, 4, 3] result = True for index in range(len(strings)): if len(strings[index]) != expected_lengths[index]: ...

Read More

Python program to Sort a List of Strings by the Number of Unique Characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 448 Views

When it is required to sort a list of strings based on the number of unique characters, a method is defined that uses a set operator, the list method and the len method. Example Below is a demonstration of the same − def my_sort_func(my_elem): return len(list(set(my_elem))) my_list = ['python', "Will", "Hi", "how", 'fun', 'learn', 'code'] print("The list is : ") print(my_list) my_list.sort(key = my_sort_func) print("The result is :") print(my_list) Output The list is : ['python', 'Will', 'Hi', 'how', 'fun', 'learn', 'code'] The result is ...

Read More

Python Program that extract words starting with Vowel From A list

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

When you need to extract words starting with vowels from a list, Python offers several approaches. You can use iteration with the startswith() method, list comprehensions, or the filter() function. Using Loop with startswith() Method This approach uses a flag to check if each word starts with any vowel ? words = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is:") print(words) result = [] vowels = "aeiou" print("The vowels are:", vowels) for word in words: flag = False for vowel in vowels: ...

Read More

Python program to print Rows where all its Elements' frequency is greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 286 Views

When working with nested lists (2D arrays), you may need to filter rows where every element appears more than K times within that row. Python provides an elegant solution using the all() function combined with list comprehension. Understanding the Problem We want to find rows where each element's frequency within that specific row exceeds a threshold value K. For example, in row [11, 11, 11, 11], the element 11 appears 4 times, so its frequency is 4. Solution Using Custom Function Here's a complete program that defines a helper function to check frequency conditions ? ...

Read More

Python Program to repeat elements at custom indices

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 218 Views

When working with lists, you might need to repeat certain elements at specific positions. This can be achieved using Python's enumerate() function along with the extend() and append() methods to create a new list with duplicated elements at custom indices. Basic Approach Using enumerate() The most straightforward method is to iterate through the original list and check if the current index should have its element repeated ? original_list = [34, 56, 77, 23, 31, 29, 62, 99] print("The original list is:") print(original_list) indices_to_repeat = [3, 1, 4, 6] result = [] for index, element ...

Read More

Python – Inverse Dictionary Values List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 528 Views

Inverting a dictionary means swapping keys and values. When the original dictionary has lists as values, we create a new dictionary where each item in those lists becomes a key, and the original keys become the values. Using defaultdict The defaultdict from collections module automatically creates empty lists for new keys ? from collections import defaultdict my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]} print("The original dictionary is:") print(my_dict) inverted_dict = defaultdict(list) for key, values in my_dict.items(): for value in values: ...

Read More

Python – Sort String list by K character frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 502 Views

When it is required to sort a list of strings based on the K character frequency, the sorted() method and lambda function can be used. This technique counts how many times a specific character appears in each string and sorts accordingly. Syntax sorted(iterable, key=lambda string: -string.count(character)) Example Below is a demonstration of sorting strings by character frequency ? string_list = ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] print("Original list:") print(string_list) K = 'l' print(f"Sorting by character '{K}' frequency:") # Sort by K character frequency (descending order) result = sorted(string_list, key=lambda ...

Read More
Showing 3911–3920 of 61,299 articles
« Prev 1 390 391 392 393 394 6130 Next »
Advertisements