Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python – Filter immutable rows representing Dictionary Keys from Matrix
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 MorePython – Extract Particular data type rows
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 MorePython – Group Consecutive elements by Sign
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 MorePython – K middle elements
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 MorePython – Filter Sorted Rows
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 MorePython – Sort row by K multiples
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 MorePython – Trim tuples by K
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 MorePython – Sort by range inclusion
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 MorePython – Maximum in Row Range
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 MorePython – Sort Matrix by Number of elements greater than its previous element
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