Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Combine list with other list elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 252 Views

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

Python program to sort matrix based upon sum of rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 302 Views

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

Python – Sort by Maximum digit in Element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 255 Views

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

Python – Calculate the percentage of positive elements of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 432 Views

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

Python – Filter Rows with Range Elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 313 Views

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

Python program to Sort Strings by Punctuation count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 328 Views

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

Python – Sort Matrix by Row Median

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 295 Views

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

Python – All combinations of a Dictionary List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

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

Python – Average digits count in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 276 Views

When it is required to count average digits in a list, a simple iteration, the str() method and the division operator is used to convert numbers to strings and calculate the mean digit count. Below is a demonstration of the same − Example my_list = [324, 5345, 243, 746, 432, 463, 946787] print("The list is :") print(my_list) sum_digits = 0 for ele in my_list: sum_digits += len(str(ele)) my_result = sum_digits / len(my_list) print("The result is :") print(my_result) Output The ...

Read More

Python – Test String in Character List and vice-versa

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 332 Views

When checking if a string exists within a character list or vice-versa, Python provides several approaches using the in operator and join() method. This is useful for string validation and pattern matching. Testing String in Character List You can check if all characters of a string exist in a character list by joining the list and using the in operator ? my_string = 'python' print("The string is:") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] print("The character list is:") print(my_key) joined_list = ''.join(my_key) print("Joined string:", joined_list) my_result = ...

Read More
Showing 3921–3930 of 61,297 articles
« Prev 1 391 392 393 394 395 6130 Next »
Advertisements