Programming Articles

Page 351 of 2547

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 317 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 210 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

Python – Test for desired String Lengths

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 233 Views

When it is required to test for desired string lengths, a simple iteration and the len() method can be used. This technique helps verify that each string in a list matches its corresponding expected length. Using Basic Iteration Below is a demonstration of checking string lengths using a for loop ? strings = ["python", "is", "fun", "to", "learn", "Will", "how"] print("The list is :") print(strings) expected_lengths = [6, 2, 3, 2, 5, 4, 3] result = True for index in range(len(strings)): if len(strings[index]) != expected_lengths[index]: ...

Read More

Python program to Sort a List of Strings by the Number of Unique Characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 371 Views

When it is required to sort a list of strings based on the number of unique characters, a method is defined that uses a set operator, the list method and the len method. Example Below is a demonstration of the same − def my_sort_func(my_elem): return len(list(set(my_elem))) my_list = ['python', "Will", "Hi", "how", 'fun', 'learn', 'code'] print("The list is : ") print(my_list) my_list.sort(key = my_sort_func) print("The result is :") print(my_list) Output The list is : ['python', 'Will', 'Hi', 'how', 'fun', 'learn', 'code'] The result is ...

Read More

Python Program that extract words starting with Vowel From A list

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

When you need to extract words starting with vowels from a list, Python offers several approaches. You can use iteration with the startswith() method, list comprehensions, or the filter() function. Using Loop with startswith() Method This approach uses a flag to check if each word starts with any vowel ? words = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is:") print(words) result = [] vowels = "aeiou" print("The vowels are:", vowels) for word in words: flag = False for vowel in vowels: ...

Read More
Showing 3501–3510 of 25,466 articles
« Prev 1 349 350 351 352 353 2547 Next »
Advertisements