Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

Python - Print rows from the matrix that have same element at a given index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 406 Views

When working with matrices (lists of lists), you may need to filter rows where all elements have the same digit at a specific position. This can be achieved using list comprehension with the all() function to check if elements share a common digit at a given index position. Problem Statement Given a matrix and an index position, find all rows where every element has the same digit at that specific index position when converted to string. Example Let's find rows where all elements have the same digit at index position 1 ? matrix = ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 449 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
AmitDiwan
Updated on 26-Mar-2026 352 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
AmitDiwan
Updated on 26-Mar-2026 236 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
AmitDiwan
Updated on 26-Mar-2026 464 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
AmitDiwan
Updated on 26-Mar-2026 391 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 492 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 226 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 258 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
Showing 3961–3970 of 61,298 articles
« Prev 1 395 396 397 398 399 6130 Next »
Advertisements