Python – Average digits count in a List

AmitDiwan
Updated on 26-Mar-2026 01:05:08

283 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
Updated on 26-Mar-2026 01:04:54

349 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

Python – Test for all Even elements in the List for the given Range

AmitDiwan
Updated on 26-Mar-2026 01:04:35

255 Views

When it is required to test all even elements in the list for the given range, a simple iteration and the modulus operator is used. Example Below is a demonstration of the same − my_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, j = 2, 7 my_result = True for index in range(i, j + 1): if my_list[index] % 2: my_result = False break ... Read More

Python – Sort Matrix by Maximum String Length

AmitDiwan
Updated on 26-Mar-2026 01:04:20

348 Views

When working with matrices (lists of lists) containing strings, you might need to sort rows based on the maximum string length in each row. Python provides an elegant solution using the sort() method with a custom key function. Example Here's how to sort a matrix by the maximum string length in each row ? def max_length(row): return max([len(element) for element in row]) my_matrix = [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] print("The matrix is:") print(my_matrix) my_matrix.sort(key=max_length) print("The result is:") print(my_matrix) The matrix is: [['pyt', 'fun'], ... Read More

Python – Extract Row with any Boolean True

AmitDiwan
Updated on 26-Mar-2026 01:04:04

262 Views

When working with nested lists or tuples containing Boolean values, you may need to extract only those rows that contain at least one True value. Python's any() function combined with list comprehension provides an elegant solution for this task. Syntax result = [row for row in data if any(row)] Example Here's how to extract rows containing at least one Boolean True ? my_data = [[False, True], [False, False], [True, False, True], [False]] print("The original data is:") print(my_data) my_result = [row for row in my_data if any(element for element in row)] ... Read More

Python – Sort Tuples by Total digits

AmitDiwan
Updated on 26-Mar-2026 01:03:46

395 Views

When working with tuples containing numeric data, you might need to sort tuples by the total number of digits across all elements. This involves counting digits in each tuple and using that count as the sorting key. Method: Using a Custom Function We can define a function that converts each element to a string, counts the digits, and returns the total ? def count_tuple_digits(row): return sum([len(str(element)) for element in row]) my_tuple = [(32, 14, 65, 723), (13, 26), (12345, ), (137, 234, 314)] print("The original tuple is:") print(my_tuple) my_tuple.sort(key=count_tuple_digits) ... Read More

Python – Incremental Slice concatenation in String list

AmitDiwan
Updated on 26-Mar-2026 01:03:30

231 Views

When working with string lists, sometimes we need to perform incremental slice concatenation where each string contributes an increasing number of characters. This technique uses list iteration and string slicing to build a concatenated result. Example Here's how to perform incremental slice concatenation ? my_list = ['pyt', 'is', 'all', 'fun'] print("The list is :") print(my_list) my_result = '' for index in range(len(my_list)): my_result += my_list[index][:index + 1] print("The result is :") print(my_result) The output of the above code is ? The list is : ['pyt', ... Read More

Python – Filter rows with required elements

AmitDiwan
Updated on 26-Mar-2026 01:03:14

392 Views

When it is required to filter rows with required elements, a list comprehension and the all() operator can be used to check if all elements in each row are present in a reference list. Example The following example demonstrates how to filter rows where all elements are present in a check list ? my_list = [[261, 49, 61], [27, 49, 3, 261], [261, 49, 85], [1, 1, 9]] print("The list is :") print(my_list) check_list = [49, 61, 261, 85] my_result = [row for row in my_list if all(element in check_list for element in ... Read More

Python – Sort by a particular digit count in elements

AmitDiwan
Updated on 26-Mar-2026 01:03:00

299 Views

When it is required to sort by a particular digit count in elements, a method is defined that takes a list element as parameter and uses the count() and str() methods to determine the results. Below is a demonstration of the same − Example def sort_count_digits(element): return str(element).count(str(my_key)) my_list = [4522, 2452, 1233, 2465] print("The list is :") print(my_list) my_key = 2 print("The value of key is") print(my_key) my_list.sort(key=sort_count_digits) print("The result is :") print(my_list) Output The list is : [4522, 2452, 1233, ... Read More

Python – Extract elements with equal frequency as value

AmitDiwan
Updated on 26-Mar-2026 01:02:45

273 Views

When it is required to extract elements with equal frequency as value, a list comprehension, the count() method and the set() operator are used. This technique finds elements where the number of times they appear in the list equals their actual value. For example, if the number 4 appears exactly 4 times in the list, or 2 appears exactly 2 times, these elements would be extracted. Example Below is a demonstration of extracting elements with equal frequency as value − my_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The ... Read More

Advertisements