Programming Articles

Page 350 of 2547

Python – List Elements Grouping in Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 357 Views

When working with matrices (list of lists), you sometimes need to group elements based on certain criteria. This example demonstrates how to group list elements in a matrix using iteration, the pop() method, list comprehension, and append() methods. Example Below is a demonstration of grouping matrix elements ? my_list = [[14, 62], [51, 23], [12, 62], [78, 87], [41, 14]] print("The list is :") print(my_list) check_list = [14, 12, 41, 62] print("The check list is :") print(check_list) my_result = [] while my_list: sub_list_1 = my_list.pop() ...

Read More

Python program to convert tuple into list by adding the given string after every element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 323 Views

When it is required to convert a tuple into a list by adding a given string after every element, list comprehension provides an elegant solution. This technique creates a new list where each original element is followed by the specified string. Example Below is a demonstration of the same − my_tuple = ((15, 16), 71, 42, 99) print("The tuple is :") print(my_tuple) K = "Pyt" print("The value of K is :") print(K) my_result = [element for sub in my_tuple for element in (sub, K)] print("The result is :") print(my_result) Output ...

Read More

Python – Cross Join every Kth segment

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 283 Views

Cross joining every Kth segment means alternating between two lists by taking K elements from the first list, then K elements from the second list, and repeating this pattern. This creates a merged sequence that preserves segments of each original list. Using Generator Function A generator function can efficiently yield elements by alternating between lists in K-sized segments ? def cross_join_kth_segment(list1, list2, k): index1 = 0 index2 = 0 while index1 < len(list1) and index2 < len(list2): ...

Read More

Python program to find the character position of Kth word from a list of strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 415 Views

When it is required to find the character position of Kth word from a list of strings, a list comprehension along with enumerate() is used to get the position of each character when all strings are concatenated. Example Below is a demonstration of the same − my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [element[0] for sub in enumerate(my_list) for element in enumerate(sub[1])] my_result = my_result[K] print("The result is :") print(my_result) Output ...

Read More

Python program to replace all Characters of a List except the given character

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 533 Views

When it is required to replace all characters of a list except a given character, a list comprehension and the == operator are used. This technique allows you to preserve specific characters while replacing all others with a substitute character. Using List Comprehension The most Pythonic approach uses a conditional list comprehension to check each element ? characters = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is:") print(characters) replace_char = '$' retain_char = 'P' result = [element if element == retain_char else replace_char for element in characters] print("The ...

Read More

Python program to concatenate Strings around K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 230 Views

String concatenation around a delimiter involves combining strings that appear before and after a specific character K. This technique is useful for merging related string elements separated by a common delimiter. Syntax The general approach uses iteration to identify patterns where strings are separated by the delimiter K ? while index < len(list): if list[index + 1] == K: combined = list[index] + K + list[index + 2] index += 2 result.append(element) ...

Read More

Python program for sum of consecutive numbers with overlapping in lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 718 Views

When summing consecutive numbers with overlapping elements in lists, we create pairs of adjacent elements where each element (except the last) is paired with its next neighbor. The last element pairs with the first element to create a circular pattern. Example Below is a demonstration using list comprehension with zip ? my_list = [41, 27, 53, 12, 29, 32, 16] print("The list is :") print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] print("The result is :") print(my_result) Output The list is : [41, ...

Read More

Python program to remove rows with duplicate element in Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 340 Views

When it is required to remove rows with duplicate elements in a matrix, a list comprehension and the set operator is used to filter out rows containing duplicate values. Understanding the Problem A matrix (list of lists) may contain rows where elements are repeated. We need to identify and remove such rows, keeping only those with unique elements ? # Example matrix with some rows having duplicate elements matrix = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("Original matrix:") for i, row in enumerate(matrix): print(f"Row {i}: ...

Read More

Python program to sort strings by substring range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 539 Views

Sorting strings by substring range allows you to order a list based on specific character positions within each string. Python provides an efficient way to achieve this using the sort() method with a custom key function. Basic Example Here's how to sort strings based on characters at positions 1 to 3 − def get_substring(my_string): return my_string[1:3] words = ["python", "is", "fun", "to", "learn"] print("Original list:") print(words) print("Substring range: positions 1-3") print("Substrings used for sorting:") for word in words: print(f"'{word}' → '{word[1:3]}'") words.sort(key=get_substring) print("Sorted ...

Read More

Python – Extract String elements from Mixed Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 459 Views

When working with mixed matrices containing different data types, you often need to extract only string elements. Python provides the isinstance() function to check data types, which can be combined with list comprehension for efficient filtering. Understanding Mixed Matrices A mixed matrix is a nested list containing elements of different data types like integers, strings, and floats in the same structure. Using isinstance() with List Comprehension The most efficient approach is to use list comprehension with isinstance() to filter string elements ? my_list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] ...

Read More
Showing 3491–3500 of 25,466 articles
« Prev 1 348 349 350 351 352 2547 Next »
Advertisements