AmitDiwan has Published 10744 Articles

Python – Extract range of Consecutive similar elements ranges from string list

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:43:28

277 Views

When it is required to extract range of consecutive similar elements ranges from list, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [12, 23, 23, 23, 48, 48, 36, 17, 17] print("The list is : ") print(my_list) my_result = ... Read More

Python – Filter Tuples with Strings of specific characters

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:41:53

250 Views

When it is required to filter tuples with strings that have specific characters, a list comprehension and the ‘all’ operator is used.ExampleBelow is a demonstration of the same −my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index ... Read More

Python – Filter rows with Elements as Multiple of K

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:40:16

165 Views

When it is required to filter rows with elements which are multiples of K, a list comprehension and modulus operator are used.ExampleBelow is a demonstration of the same −my_list = [[15, 10, 25], [14, 28, 23], [120, 55], [55, 30, 203]] print("The list is :") print(my_list) K = ... Read More

Python – Character indices Mapping in String List

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:38:59

577 Views

When it is required to find character indices that map to a string list, a simple iteration, list comprehension and ‘add’ method is used.ExampleBelow is a demonstration of the same −from collections import defaultdict my_list = ['p y t h o n', 'i s', 'f u n', 't o', ... Read More

Python – Extract dictionaries with values sum greater than K

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:37:29

493 Views

When it is required to extract dictionaries with values sum greater than K, a simple iteration and ‘if’ condition is used.ExampleBelow is a demonstration of the same −my_list = [{"Python" : 14, "is" : 18, "fun" : 19}, {"Python" : 12, "is": 4, "fun" : 16}, {"Python" : 13, "is": ... Read More

Python – Mapping Matrix with Dictionary

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:35:14

983 Views

When it is required to map the matrix to a dictionary, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("The list :") print(my_list) map_dict = {2 : "Python", 1: "fun", 3 : "to", ... Read More

Python – Convert Integer Matrix to String Matrix

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:33:42

342 Views

When it is required to convert an integer matrix to a string matrix, a list comprehension is used.ExampleBelow is a demonstration of the same −my_list = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("The list is :") print(my_list) my_result = [[str(element) for element ... Read More

Python program to extract characters in given range from a string list

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:24:21

478 Views

When it is required to extract characters in given range from a string list, a list comprehension and list slicing is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ... Read More

Python – Sort a List by Factor count

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:22:49

413 Views

When it is required to sort a list by factor count, a method is defined that uses list comprehension and modulus operator along with ‘len’ method to determine the output.ExampleBelow is a demonstration of the same −def factor_count(element):    return len([element for index in range(1, element) if element % index ... Read More

Python – Random range in a List

AmitDiwan

AmitDiwan

Updated on 08-Sep-2021 10:21:24

934 Views

When it is required to find the random range in a list, a list comprehension and the ‘randrange’ method present in ‘random’ package is used.ExampleBelow is a demonstration of the same −import random my_result = [random.randrange(1, 100, 1) for i in range(10)] print ("The result is :") print(my_result)OutputThe ... Read More

Advertisements