Found 26504 Articles for Server Side Programming

Python program to get the indices of each element of one list in another list

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

252 Views

When it is required to get the indices of each element of one list in another list, a simple iteration and the enumerate attribute along with the ‘setdefault’ method is used.It also uses list comprehension and the ‘get’ method is used.ExampleBelow is a demonstration of the same −my_list = [14, 52, 23, 47, 18, 23, 12, 54, 43, 22, 28, 13] print("The list is :") print(my_list) my_list_2 = [17, 52, 13] print("The second list is :") print(my_list_2) element_indices = dict() for index, value in enumerate(my_list): element_indices.setdefault(value, []).append(index) my_result = [element_indices.get(index, [None]) for index ... Read More

Python program to remove rows with duplicate element in Matrix

AmitDiwan
Updated on 08-Sep-2021 07:10:25

278 Views

When it is required to remove rows with duplicate element in a matrix, a list comprehension and the ‘set’ operator is used.ExampleBelow is a demonstration of the same −my_list = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("The list is :") print(my_list) my_result = [element for element in my_list if len(set(element)) == len(element)] print("The result is :") print(my_result)OutputThe list is : [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] The result is : [[17, 46, 47], [28, 91, 19]]ExplanationA list of list is defined and is displayed on ... Read More

Python program to extract Keywords from a list

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

822 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

475 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

388 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

224 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

150 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

Advertisements