Found 33676 Articles for Programming

Python program to extract Keywords from a list

AmitDiwan
Updated on 08-Sep-2021 07:09:22

823 Views

When it is required to extract keywords from a list, a simple iteration and the ‘iskeyword’ method is used.ExampleBelow is a demonstration of the same −import keyword my_list = ["python", 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_result = [] for element in my_list: for word in element.split(): if keyword.iskeyword(word): my_result.append(word) print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ['is']ExplanationA list of strings is defined and ... Read More

Python program to sort strings by substring range

AmitDiwan
Updated on 08-Sep-2021 07:07:39

479 Views

When it is required to sort the strings based on a substring range, a method us defined that uses list slicing to determine the result.ExampleBelow is a demonstration of the same −def get_substring(my_string): return my_string[i : j] my_list = ["python", 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) i, j = 1, 3 print("The value of i and j are :") print(str(i)+ ', ' +str(j)) my_list.sort(key=get_substring) print("The result is :") print(my_list)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The value of i and j are : 1, 3 The result ... Read More

Python – Extract String elements from Mixed Matrix

AmitDiwan
Updated on 08-Sep-2021 07:06:36

389 Views

When it is required to extract string elements from mixed matrix, a list comprehension and the ‘isinstance’ method is used.ExampleBelow is a demonstration of the same −my_list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] print("The list is :") print(my_list) my_result = [element for index in my_list for element in index if isinstance(element, str)] print("The result is :") print(my_result)OutputThe list is : [[35, 66, 31], ['python', 13, 'is'], [15, 'fun', 14]] The result is : ['python', 'is', 'fun']ExplanationA list of list is defined and displayed on the console.A list comprehension is used to iterate over ... Read More

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

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

226 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) N = 99 print("The value of N is :") print(N) for index in range(K): my_list[index] = N print("The result is :") print(my_list)OutputThe list is : [13, 34, 26, 58, 14, 32, 16, 89] The value of K is : 2 The value of N ... Read More

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

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

248 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 range(0, len(my_string), my_key): my_result.append(my_string[:index] + my_char + my_string[index:]) return str(my_result) my_string = 'PythonToCode' print("The string is :") print(my_string) K = 2 print("The value of K is ") print(K) add_char = ";" print("The result is ... Read More

Python – Average of digit greater than K

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

152 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 for index in my_list :    if index > K :       my_count = my_count + 1 print ("The result is :") print(my_count)OutputThe list is : [11, 17, 25, 16, 23, 18] The value of K is 15 The result is : 5ExplanationA list is defined and displayed ... Read More

Python – Next N elements from K value

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

296 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 = 3 print("The value of N is :") print(N) for index in range(K):    my_list[index] = N print("The result is :") print(my_list)OutputThe list is : [31, 24, 46, 18, 34, 52, 26, 29] The value of K is : 2 The value of N is : 3 The result is : ... Read More

Python – Create dictionary from the list

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'] {'Hi': 'my_value', 'Will': 'my_value', 'how': 'my_value', 'Python': 'my_value', 'cool': 'my_value'}ExplanationA list of strings is defined and is displayed on the console.The ‘fromkeys’ method present in ‘dict’ is used to convert the elements of the list to dictionary keys.The value for every key is specified here itself.This is assigned to a ... Read More

Python – Remove Tuples with difference greater than K

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

153 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 – Remove dictionary from a list of dictionaries if a particular value is not present

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" : "Code"}, {"id" : 3, "data" : "Learn"}] print("The list is :") print(my_list) for index in range(len(my_list)): if my_list[index]['id'] == 2: del my_list[index] break print("The result is :") print(my_list)OutputThe list ... Read More

Advertisements