When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used. This technique alternates between summing the first element of tuples at even indices and the second element of tuples at odd indices. Below is a demonstration of the same − Example my_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)): if index % 2 == 0: my_result += ... Read More
When it is required to extract rear K digits from numbers, a simple list comprehension, the modulo operator and the ** operator are used. The modulo operator with 10**K effectively extracts the last K digits from any number. How It Works The formula number % (10**K) extracts the rear K digits because: 10**K creates a number with K+1 digits (e.g., 10³ = 1000) The modulo operation returns the remainder when dividing by this value This remainder is always less than 10**K, giving us exactly the last K digits Example Let's extract the last ... Read More
When it is required to combine list with other list elements, Python provides several methods. The most common approaches are using iteration with append(), the extend() method, or the + operator. Method 1: Using Loop with append() This method iterates through the second list and appends each element to the first list ? my_list_1 = [12, 14, 25, 36, 15] print("The first list is :") print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print("The second list is :") print(my_list_2) for element in my_list_2: my_list_1.append(element) print("The result is :") print(my_list_1) ... Read More
When it is required to sort a matrix based upon the sum of rows, we can use Python's built-in sort() method with a custom key function. The key function calculates the sum of each row, and the matrix is sorted in ascending order based on these sums. Method 1: Using a Custom Function Define a helper function that calculates the sum of a row and use it as the key for sorting ? def sort_sum(row): return sum(row) matrix = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The ... Read More
When it is required to sort by maximum digit in element, a method is defined that uses str() and max() method to determine the result. This technique converts each number to a string, finds its largest digit, and uses that digit as the sorting key. Syntax def max_digits(element): return max(str(element)) list.sort(key=max_digits) Example Here's how to sort a list of numbers by their maximum digit ? def max_digits(element): return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) ... Read More
When calculating the percentage of positive elements in a list, we can use list comprehension along with the len() method to count positive values and compute the percentage. Example Here's how to calculate the percentage of positive elements in a list ? my_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result) Output The list is : [14, 62, -22, 13, -87, ... Read More
When working with nested lists, you may need to filter rows that contain all elements from a specific range. Python provides an elegant solution using list comprehension with the all() function to check if every element in a range exists within each row. Syntax filtered_rows = [row for row in nested_list if all(element in row for element in range(start, end + 1))] Example Let's filter rows that contain all numbers from 2 to 5 ? my_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, ... Read More
When you need to sort strings by their punctuation count, you can define a custom function that counts punctuation marks and use it as a sorting key. This technique uses Python's string.punctuation module and list comprehension to efficiently count punctuation characters. Example Here's how to sort strings based on the number of punctuation marks they contain − from string import punctuation def get_punctuation_count(my_str): return len([element for element in my_str if element in punctuation]) my_list = ["python@%^", "is", "fun!", "to@#r", "@#$learn!"] print("The list is :") print(my_list) my_list.sort(key = get_punctuation_count) ... Read More
When working with matrices in Python, you may need to sort rows based on their median values. Python's statistics module provides a median() function that can be used with the sort() method's key parameter to achieve this. Syntax from statistics import median matrix.sort(key=lambda row: median(row)) Example Here's how to sort a matrix by row median using a custom function ? from statistics import median def median_row(row): return median(row) my_matrix = [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] print("Original matrix:") ... Read More
When you need to create all possible combinations of dictionaries from keys and values, Python's itertools.product function combined with list comprehension provides an elegant solution. This technique generates the Cartesian product of values and maps them to corresponding keys. Basic Example Here's how to create all combinations where keys from one list are paired with all possible value combinations from another list ? from itertools import product keys = ["python", "is", "fun"] values = [24, 15] print("The keys list is:") print(keys) print("The values list is:") print(values) # Generate all combinations using product temp ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance