AmitDiwan has Published 10744 Articles

Python program to replace first ‘K’ elements by ‘N’

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 07:05:38

223 Views

When it is required to replace first ‘K’ elements by ‘N’, a simple iteration is used.ExampleBelow is a demonstration of the same −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) ... Read More

Python – Average of digit greater than K

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 07:02:22

148 Views

When it is required to display average of digit greater than K, a simple iteration is used.Below is a demonstration of the same −Examplemy_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_count = 0 ... Read More

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

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 07:01:59

245 Views

When it is required to insert character in each duplicate string after every ‘K’ elements, a method is defined that uses ‘append’ method, concatenation operator and list slicing.ExampleBelow is a demonstration of the same −def insert_char_after_key_elem(my_string, my_key, my_char): my_result = [] for index in ... Read More

Python – Next N elements from K value

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:51:20

293 Views

When it is required to get next N elements from K value, a simple iteration is used.Below is a demonstration of the same −Examplemy_list = [31, 24, 46, 18, 34, 52, 26, 29] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = ... Read More

Python – Create dictionary from the list

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:48:26

2K+ Views

When it is required to create dictionary from a list, the ‘fromkeys’ method in the ‘dict’ method is used.ExampleBelow is a demonstration of the same −my_list = ['Hi', 'Will', 'how', 'Python', 'cool'] print("The list is ") print(my_list) my_dict = dict.fromkeys(my_list, "my_value") print(my_dict)OutputThe list is ['Hi', 'Will', 'how', 'Python', 'cool'] ... Read More

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

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:46:39

4K+ Views

When it is required to remove dictionary from a list of dictionaries if a particular value is not present, a simple iteration and the ‘del’ operator is used.ExampleBelow is a demonstration of the same −my_list = [{"id" : 1, "data" : "Python"}, {"id" : 2, "data" : ... Read More

Python – Remove Tuples with difference greater than K

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:45:23

151 Views

When it is required to remove tuples with difference greater than K, use the abs() method.Below is a demonstration of the same −Examplemy_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 – All occurrences of Substring from the list of strings

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:41:51

297 Views

When it is required to get all the occurrences of a substring from the list of strings, a simple list comprehension and the ‘startswith’ method is used.ExampleBelow is a demonstration of the same −my_string = "Is python fun to learn?" print("The list is :") print(my_string) substring = "pyt" ... Read More

Python – Extract Strings with Successive Alphabets in Alphabetical Order

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 06:39:12

182 Views

When it is required to extract strings which have successive alphabets in alphabetical order, a simple iteration, and the ‘ord’ method for Unicode representation is used.ExampleBelow is a demonstration of the same −my_list = ["python", 'is', 'cool', 'hi', 'Will', 'How'] print("The list is :") print(my_list) my_result = [] ... Read More

Python Program that extract words starting with Vowel From A list

AmitDiwan

AmitDiwan

Updated on 07-Sep-2021 19:52:11

2K+ Views

When it is required to extract words starting with vowel from a list, a simple iteration, a flag value and the ‘startswith’ method are used.Below is a demonstration of the same −Example:my_list = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is :") print(my_list) my_result = [] my_vowel = ... Read More

Advertisements