Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Filter Strings within ASCII range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 404 Views

When working with text data, you may need to verify if all characters in a string fall within the ASCII range (0-127). Python provides the ord() function to get Unicode values and all() to check conditions across all elements. Understanding ASCII Range ASCII characters have Unicode values from 0 to 127. This includes letters (A-Z, a-z), digits (0-9), punctuation, and control characters. Characters with Unicode values 128 and above are non-ASCII. Method 1: Using all() with ord() The most concise approach uses all() with a generator expression ? my_string = "Hope you are well" ...

Read More

Python – Remove strings with any non-required character

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 176 Views

When working with string filtering in Python, you often need to remove strings that contain unwanted characters. Python provides several approaches to filter out strings containing specific characters using list comprehension with the any() function. Using List Comprehension with any() The any() function returns True if any element in an iterable is True. Combined with not any(), we can filter strings that don't contain unwanted characters ? words = ["python", "is", "fun", "to", "learn"] print("The word list is:") print(words) unwanted_chars = ['p', 's', 'l'] print("The unwanted characters are:") print(unwanted_chars) filtered_words = [word ...

Read More

Python – Extract rows with Even length strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 202 Views

When working with nested lists of strings, you may need to extract rows where all strings have even length. This can be accomplished using list comprehension with the all() function and modulus operator %. Syntax result = [row for row in nested_list if all(len(element) % 2 == 0 for element in row)] How It Works The solution uses three key components ? len(element) % 2 == 0 − Checks if string length is even all() − Returns True only if all elements in the row have even length List comprehension − Filters ...

Read More

Python – Test if Rows have Similar frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 214 Views

When checking if rows have similar frequency in a list of lists, you can use Python's Counter class along with the all() function to compare element frequencies across all rows. Example Here's how to test if all rows contain the same elements with identical frequencies ? from collections import Counter my_list = [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] print("The list is :") print(my_list) my_result = all(dict(Counter(row)) == dict(Counter(my_list[0])) for row in my_list) if my_result == True: print("All rows have ...

Read More

Python Program that filters out non-empty rows of a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 261 Views

When working with matrices (lists of lists), you often need to filter out empty rows. Python provides several approaches to remove non-empty rows from a matrix using list comprehension, the filter() function, or loops. Using List Comprehension List comprehension with len() provides a clean and readable solution ? my_matrix = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], [9, 2]] print("The original matrix is:") print(my_matrix) # Filter out empty rows using list comprehension filtered_matrix = [row for row in my_matrix if len(row) > 0] print("The matrix after filtering empty rows:") print(filtered_matrix) ...

Read More

Python – Test if all elements are unique in columns of a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 527 Views

When it is required to test if all elements are unique in columns of a matrix, a simple iteration and a list comprehension along with the 'set' operator are used. Below is a demonstration of the same − Example my_matrix = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The matrix is:") print(my_matrix) my_result = True for index in range(len(my_matrix[0])): column = [row[index] for row in my_matrix] if len(list(set(column))) != len(column): ...

Read More

Python – Remove characters greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 246 Views

When working with strings, you may need to filter out characters based on their position in the alphabet. This technique removes characters that appear after a certain position K in the alphabet using the ord() function to get Unicode values. Understanding the Approach The method compares each character's alphabetical position with K. Since lowercase 'a' has ASCII value 97, we subtract 97 from each character's ASCII value to get its alphabetical position (a=0, b=1, c=2, etc.). Example Here's how to remove characters greater than K from a list of strings ? words = ["python", ...

Read More

Python – Check if any list element is present in Tuple

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

When it is required to check if any list element is present in a tuple or not, Python provides several approaches. We can use simple iteration, the any() function, or set intersection for efficient checking. Using Simple Iteration This approach uses a loop to check each list element against the tuple ? my_tuple = (14, 35, 27, 99, 23, 89, 11) print("The tuple is :") print(my_tuple) my_list = [16, 27, 88, 99] print("The list is :") print(my_list) my_result = False for element in my_list: if element in my_tuple: ...

Read More

Python Program to sort rows of a matrix by custom element count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 264 Views

When sorting rows of a matrix based on custom element count, we can define a helper function that counts how many elements from each row match our custom criteria. This approach uses list comprehension with the len() method to determine the sort order. Example Let's sort matrix rows by counting how many elements from each row appear in our custom list ? def get_count_matrix(my_key): return len([element for element in my_key if element in custom_list]) my_list = [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] print("The list is ...

Read More

Python Program to Filter Rows with a specific Pair Sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 256 Views

When working with lists of numbers, we often need to filter rows that contain a specific pair sum. This involves checking if any two elements in each row add up to a target value and keeping only those rows that satisfy this condition. Problem Overview Given a list of lists (rows) and a target sum, we want to filter and keep only the rows where at least one pair of elements adds up to the target value ? Method: Using Helper Function with List Comprehension We'll create a helper function to check for pair sums and ...

Read More
Showing 3971–3980 of 61,297 articles
« Prev 1 396 397 398 399 400 6130 Next »
Advertisements