Server Side Programming Articles

Page 353 of 2109

Python – Consecutive Division in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 298 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 208 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 428 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 359 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 459 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 236 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 356 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
Showing 3521–3530 of 21,090 articles
« Prev 1 351 352 353 354 355 2109 Next »
Advertisements