Found 10476 Articles for Python

Python program to convert tuple into list by adding the given string after every element

AmitDiwan
Updated on 08-Sep-2021 08:17:52

256 Views

When it is required to convert tuple into list by adding the given string after every element, the list comprehension is used.ExampleBelow is a demonstration of the same −my_tuple = ((15, 16), (71), 42, 99) print("The tuple is :") print(my_tuple) K = "Pyt" print("The value of K is :") print(K) my_result = [element for sub in my_tuple for element in (sub, K)] print("The result is :") print(my_result)OutputThe tuple is : ((15, 16), 71, 42, 99) The value of K is : Pyt The result is : [(15, 16), 'Pyt', 71, 'Pyt', 42, 'Pyt', 99, 'Pyt']ExplanationA tuple ... Read More

Python – Cross Join every Kth segment

AmitDiwan
Updated on 08-Sep-2021 07:32:15

227 Views

When it is required to cross join every ‘K’th element, a method is defined that uses iteration and fetches the index as output.ExampleBelow is a demonstration of the same −def merge_pair_elem(my_list_1, my_list_2, K): index_1 = 0 index_2 = 0 while(index_1 < len(my_list_1)): for i in range(K): yield my_list_1[index_1] index_1 += 1 for i in range(K): ... Read More

Python program to find the character position of Kth word from a list of strings

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

354 Views

When it is required to find the character position of ‘K’th word from a list of strings, a list comprehension along with enumerate is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [element[0] for sub in enumerate(my_list) for element in enumerate(sub[1])] my_result = my_result[K] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The value of K is : 15 The result is : 2ExplanationA list of strings ... Read More

Python program to replace all Characters of a List except the given character

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

467 Views

When it is required to replace all characters of a list except a given character, a list comprehension, and the ‘==’ operator are used.ExampleBelow is a demonstration of the same −my_list = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is :") print(my_list) replace_char = '$' retain_char = 'P' my_result = [element if element == retain_char else replace_char for element in my_list] print("The result is :") print(my_result)OutputThe list is : ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] The result is : ['P', '$', '$', '$', '$', '$', 'P', '$', 'P']ExplanationA ... Read More

Python program to concatenate Strings around K

AmitDiwan
Updated on 08-Sep-2021 07:16:04

172 Views

When it is required to concatenate strings around ‘K’, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = ["python", "+", 'is', 'fun', "+", 'to', 'learn'] print("The list is :") print(my_list) K = "+" print("The value of K is :") print(K) my_result = [] index = 0 while index < len(my_list): element = my_list[index] if (index < len(my_list) - 1) and my_list[index + 1] == K: element = element + K + my_list[index + 2] ... Read More

Python program for sum of consecutive numbers with overlapping in lists

AmitDiwan
Updated on 08-Sep-2021 07:13:31

643 Views

When it is required to sum the consecutive numbers with overlapping elements in lists, a list comprehension, list slicing, concatenation operator and ‘zip’ methods are used.ExampleBelow is a demonstration of the same −my_list = [41, 27, 53, 12, 29, 32, 16] print("The list is :") print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] print("The result is :") print(my_result)OutputThe list is : [41, 27, 53, 12, 29, 32, 16] The result is : [68, 80, 65, 41, 61, 48, 57]ExplanationA list of integers is defined and is displayed on the console.A list ... Read More

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

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

254 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

280 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

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

476 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

Advertisements