Programming Articles

Page 357 of 2547

Python – Group Consecutive elements by Sign

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 358 Views

When working with lists of numbers, you might need to group consecutive elements by their sign (positive or negative). This can be achieved using the XOR operator (^) along with enumeration to detect sign changes between consecutive elements. Understanding the XOR Operator for Sign Detection The XOR operator (^) helps detect when two numbers have different signs. When applied to numbers with different signs, the result is negative, indicating a sign change. Example Here's how to group consecutive elements by their sign ? numbers = [15, -33, 12, 64, 36, -12, -31, -17, -49, ...

Read More

Python – K middle elements

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

When it is required to determine K middle elements from a list, the // operator and list slicing is used. This technique finds the center position of a list and extracts K consecutive elements around that center. Syntax # Calculate middle position middle = len(list) // 2 # Calculate start and end indices start_index = middle - (K // 2) end_index = middle + (K // 2) # Extract K middle elements result = list[start_index:end_index + 1] Example Here's how to extract 5 middle elements from a list ? my_list ...

Read More

Python – Filter Sorted Rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 458 Views

When it is required to filter sorted rows, a list comprehension and the sorted() method are used. This technique helps identify sublists that are already sorted in either ascending or descending order. What are Sorted Rows? A sorted row is a sublist where elements are arranged in either ascending order (1, 4, 15, 99) or descending order (99, 15, 4, 1). We can filter these using list comprehension ? Example my_list = [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]] print("The list is :") print(my_list) ...

Read More

Python – Sort row by K multiples

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 196 Views

When sorting rows based on the count of multiples of K, we can use a custom key function with list comprehension and the modulus operator. This approach counts how many elements in each row are divisible by K and sorts accordingly. Example Let's sort a list of sublists based on how many multiples of K each sublist contains ? def multiple_sort_val(row): return len([ele for ele in row if ele % K == 0]) my_list = [[11, 44, 7, 11], [7, 5, 44, 11], [11, 6, 35, 44], [92, 92, 5]] ...

Read More

Python – Trim tuples by K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 234 Views

When it is required to trim tuples based on a K value, a simple iteration and the 'append' method is used. Trimming removes K elements from both the beginning and end of each tuple. Example The following code demonstrates how to trim tuples by removing the first and last K elements ? my_list = [(44, 3, 68, 11, 5), (68, 44, 9, 5, 8), (8, 11, 2, 68, 5), (44, 68, 2, 5, 7)] print("The list is :") print(my_list) K = 1 print("The value for K is") print(K) my_result = [] for ...

Read More

Python – Sort by range inclusion

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 547 Views

Sometimes we need to sort a list of sublists based on how many tuples fall within a specific range. This technique uses list comprehension with abs() and sum() to calculate range inclusion scores for sorting. Syntax def range_inclusion_score(sublist, min_val, max_val): return sum([abs(tuple[1] - tuple[0]) for tuple in sublist if min_val < tuple[0] < max_val and min_val < tuple[1] < max_val]) my_list.sort(key=lambda x: range_inclusion_score(x, i, j)) Example Here's how to sort sublists based ...

Read More

Python – Maximum in Row Range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 355 Views

When it is required to find the maximum value in a row range, a simple iteration and the max() method is used. Example Below is a demonstration of finding the maximum value across specified rows ? my_list = [[11, 35, 6], [9, 11, 3], [35, 4, 2], [8, 15, 35], [5, 9, 18], [5, 14, 2]] print("The list is :") print(my_list) i, j = 2, 4 print("The row range values are") print(i, j) my_result = 0 for index in range(i, j): my_result = max(max(my_list[index]), my_result) print("The maximum value ...

Read More

Python – Sort Matrix by Number of elements greater than its previous element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 267 Views

When it is required to sort a matrix based on the number of elements that is greater than the previous element, a list comprehension and the len() method is used with a custom function as the sorting key. Example Here's how to sort a matrix by counting ascending pairs in each row ? def fetch_greater_freq(row): return len([row[idx] for idx in range(0, len(row) - 1) if row[idx] < row[idx + 1]]) my_list = [[11, 3, 25, 99, 10], [5, 3, 25, 4], [77, 11, 5, 3, 77, 77], [11, 3, 25]] ...

Read More

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
Showing 3561–3570 of 25,466 articles
« Prev 1 355 356 357 358 359 2547 Next »
Advertisements