Found 10476 Articles for Python

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

225 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

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

Python – All occurrences of Substring from the list of strings

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

299 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" print("The substring is :") print(substring) my_result = [i for i in range(len(my_string)) if my_string.startswith(substring, i)] print("The result is :") print(my_result)OutputThe list is : Is python fun to learn? The substring is : pyt The result is : [3]ExplanationA string is defined and is displayed on the console.Another substring ... Read More

Python – Extract Strings with Successive Alphabets in Alphabetical Order

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

184 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 = [] for element in my_list: for index in range(len(element) - 1): if ord(element[index]) == ord(element[index + 1]) - 1: my_result.append(element) break print("The result ... Read More

Advertisements