Python program for sum of consecutive numbers with overlapping in lists

AmitDiwan
Updated on 26-Mar-2026 01:13:52

729 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
Updated on 26-Mar-2026 01:13:36

354 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
Updated on 26-Mar-2026 01:13:17

543 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
Updated on 26-Mar-2026 01:12:56

467 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

Python program to replace first 'K' elements by 'N'

AmitDiwan
Updated on 26-Mar-2026 01:12:37

290 Views

When it is required to replace first 'K' elements by 'N', a simple iteration is used. This operation modifies the original list by substituting the first K elements with a specified value N. Basic Approach Using Loop The simplest method uses a for loop with range() to iterate through the first K positions ? my_list = [13, 34, 26, 58, 14, 32, 16, 89] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 99 print("The value of N is :") print(N) for index in ... Read More

Python – Insert character in each duplicate string after every K elements

AmitDiwan
Updated on 26-Mar-2026 01:12:22

330 Views

When it is required to insert a character in each duplicate string after every K elements, a method is defined that uses the append method, concatenation operator and list slicing to create multiple versions of the string with the character inserted at different positions. Example Below is a demonstration of the same − def insert_char_after_key_elem(my_string, my_key, my_char): my_result = [] for index in range(0, len(my_string), my_key): my_result.append(my_string[:index] + my_char + my_string[index:]) return my_result my_string = ... Read More

Python – Average of digit greater than K

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

222 Views

When it is required to find the average of numbers greater than K in a list, we can use iteration to filter elements and calculate the mean. This involves counting qualifying elements and summing their values. Example my_list = [11, 17, 25, 16, 23, 18] print("The list is :") print(my_list) K = 15 print("The value of K is") print(K) my_sum = 0 my_count = 0 for number in my_list: if number > K: my_sum += number ... Read More

Python – Next N elements from K value

AmitDiwan
Updated on 26-Mar-2026 01:11:50

370 Views

When working with lists in Python, you might need to extract the next N elements starting from a specific position K. This operation is useful for data processing and list manipulation tasks. Understanding the Problem Given a list, a starting position K, and a count N, we want to get the next N elements from position K onwards. This means extracting elements from index K to index K+N-1. Method 1: Using List Slicing The most Pythonic way to get next N elements from position K is using list slicing ? my_list = [31, 24, ... Read More

Python – Remove Tuples with difference greater than K

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

207 Views

When working with tuples, you may need to filter out tuples where the absolute difference between elements exceeds a threshold value K. Python provides an efficient way to accomplish this using list comprehension and the abs() function. Example Here's how to remove tuples with difference greater than K ? my_tuple = [(41, 18), (21, 57), (39, 22), (23, 42), (22, 10)] print("The tuple is :") print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1])

Python – Remove dictionary from a list of dictionaries if a particular value is not present

AmitDiwan
Updated on 26-Mar-2026 01:11:16

4K+ Views

When you need to remove a dictionary from a list based on whether a particular value is present or absent, you can use several approaches. The most common methods are iteration with del, list comprehension, or the filter() function. Method 1: Using del with Index-based Iteration This method iterates through the list and removes the dictionary when a condition is met ? my_list = [{"id": 1, "data": "Python"}, {"id": 2, "data": "Code"}, {"id": 3, ... Read More

Advertisements