Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Sort by Rear Character in Strings List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 223 Views

When working with string lists in Python, you may need to sort them based on their last character. Python provides multiple approaches to achieve this using the sort() method with a custom key function. Using a Custom Function Define a function that returns the last character using negative indexing ? def get_rear_position(element): return element[-1] my_list = ['python', 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_list.sort(key=get_rear_position) print("The result is :") print(my_list) The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ...

Read More

Python – Sort Matrix by None frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 192 Views

When it is required to sort a matrix by None frequency, we can define a helper function that counts None values in each row using list comprehension and the not operator. The matrix is then sorted based on the count of None values in ascending order. Example Below is a demonstration of sorting a matrix by None frequency − def get_None_freq(row): return len([element for element in row if not element]) my_list = [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] print("The list is:") print(my_list) my_list.sort(key=get_None_freq) ...

Read More

Python – Extract range of Consecutive similar elements ranges from string list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 328 Views

When working with lists containing consecutive similar elements, you often need to extract ranges showing where each group of identical elements starts and ends. Python provides a simple approach using iteration and the append() method to identify these consecutive groups. Example Below is a demonstration of extracting consecutive similar element ranges ? my_list = [12, 23, 23, 23, 48, 48, 36, 17, 17] print("The list is:") print(my_list) my_result = [] index = 0 while index < (len(my_list)): start_position = index val = my_list[index] ...

Read More

Python – Filter Tuples with Strings of specific characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 308 Views

When it is required to filter tuples with strings that have specific characters, a list comprehension and the all() function can be used to check if all characters in each string exist within a given character set. Example Below is a demonstration of filtering tuples containing only strings whose characters are present in a specific character set − my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index for index in my_list if all(all(sub in char_string for sub in element) for element in index)] ...

Read More

Python – Filter rows with Elements as Multiple of K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 205 Views

When working with nested lists in Python, you might need to filter rows where all elements are multiples of a specific value K. This can be achieved using list comprehension combined with the all() function and modulus operator. Syntax The general syntax for filtering rows with elements as multiples of K is − result = [row for row in nested_list if all(element % K == 0 for element in row)] Example Below is a demonstration of filtering rows where all elements are multiples of K − my_list = [[15, 10, 25], ...

Read More

Python – Character indices Mapping in String List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 677 Views

When working with string lists, you may need to map each character to the indices where it appears. Python provides an efficient solution using defaultdict from the collections module combined with enumeration and set operations. Syntax from collections import defaultdict result = defaultdict(set) for index, string in enumerate(string_list): for char in string.split(): result[char].add(index + 1) Example Below is a demonstration that maps each character to its string positions ? from collections import defaultdict my_list = ['p y t ...

Read More

Python – Extract dictionaries with values sum greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 538 Views

Sometimes we need to filter a list of dictionaries based on the sum of their values. This is useful when working with data where you want to find records whose total values exceed a certain threshold. Using Loop Iteration The most straightforward approach is to iterate through each dictionary and calculate the sum of its values ? student_scores = [ {"Math": 14, "Science": 18, "English": 19}, {"Math": 12, "Science": 4, "English": 16}, {"Math": 13, "Science": 17, "English": 11}, {"Math": 13, ...

Read More

Python – Mapping Matrix with Dictionary

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

When working with matrices (lists of lists) in Python, you often need to map numerical values to meaningful strings using a dictionary. This technique is useful for converting coded data into readable format. Basic Matrix Mapping Here's how to map matrix elements using a dictionary with nested loops − my_matrix = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("Original matrix:") print(my_matrix) # Mapping dictionary map_dict = {2: "Python", 1: "fun", 3: "to", 4: "learn"} # Map matrix elements result = [] for row in my_matrix: mapped_row ...

Read More

Python – Convert Integer Matrix to String Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 398 Views

When working with matrices in Python, you may need to convert an integer matrix to a string matrix for display purposes or string operations. Python provides several approaches to accomplish this conversion efficiently. Using List Comprehension The most pythonic way is using nested list comprehension to iterate through the matrix and convert each element ? matrix = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("Original integer matrix:") print(matrix) # Convert each integer to string using nested list comprehension string_matrix = [[str(element) for element in row] for row in matrix] ...

Read More

Python program to extract characters in given range from a string list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 551 Views

When it is required to extract characters in a given range from a string list, we can use list comprehension and string slicing. This technique allows us to join all strings and extract a specific portion based on character positions. Example Below is a demonstration of extracting characters from position 11 to 25 ? my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result) Output The list ...

Read More
Showing 1–10 of 61,303 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements