Server Side Programming Articles

Page 347 of 2109

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

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 284 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
AmitDiwan
Updated on 26-Mar-2026 319 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
AmitDiwan
Updated on 26-Mar-2026 212 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
AmitDiwan
Updated on 26-Mar-2026 361 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
AmitDiwan
Updated on 26-Mar-2026 195 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])

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 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

Python – Extract Strings with Successive Alphabets in Alphabetical Order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 255 Views

When working with strings, you may need to extract strings that contain successive alphabets in alphabetical order. This means finding strings where at least two consecutive characters follow each other in the alphabet (like 'hi' where 'h' and 'i' are consecutive). Understanding Successive Alphabets Successive alphabets are characters that follow each other in the alphabet sequence. For example: 'ab' - 'a' and 'b' are successive 'hi' - 'h' and 'i' are successive 'xyz' - 'x', 'y', 'z' are all successive Method: Using ord() Function The ord() function returns the Unicode code point of a ...

Read More
Showing 3461–3470 of 21,090 articles
« Prev 1 345 346 347 348 349 2109 Next »
Advertisements