Python Program – Print the count of either peaks or valleys from a list

AmitDiwan
Updated on 26-Mar-2026 00:56:30

403 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
Updated on 26-Mar-2026 00:56:09

313 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
Updated on 26-Mar-2026 00:55:54

219 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

Python – Extract Particular data type rows

AmitDiwan
Updated on 26-Mar-2026 00:55:32

437 Views

When working with lists of lists in Python, you may need to extract rows that contain only elements of a particular data type. This can be achieved using list comprehension combined with the isinstance() method and all() operator. Syntax result = [row for row in data if all(isinstance(element, data_type) for element in row)] Example Here's how to extract rows containing only integers from a mixed-type list ? my_list = [[14, 35, "Will"], [12, 26, 17], ["p", "y", "t"], [29, 40, 21]] print("The list is:") print(my_list) my_data_type = int my_result ... Read More

Python – Group Consecutive elements by Sign

AmitDiwan
Updated on 26-Mar-2026 00:55:17

371 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
Updated on 26-Mar-2026 00:55:01

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
Updated on 26-Mar-2026 00:54:44

471 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
Updated on 26-Mar-2026 00:54:30

201 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
Updated on 26-Mar-2026 00:54:16

243 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
Updated on 26-Mar-2026 00:54:00

556 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

Advertisements