Extract Elements from Ranges in List using Python

AmitDiwan
Updated on 08-Sep-2021 08:25:53

588 Views

When it is required to extract element from ranges in a list, a simple iteration and the ‘extend’ method is used.ExampleBelow is a demonstration of the same −my_list = [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, 66, 99, 19] print("The list is :") print(my_list) range_list = [(12, 14), (17, 18), (22, 28)] print("The list is :") print(range_list) my_result = [] for element in range_list: my_result.extend(my_list[element[0] : element[1] + 1]) print("The result is :") print(my_result)OutputThe list is : [14, 55, 41, 14, 17, 59, 22, 25, 14, 69, 42, ... Read More

Fractional Frequency of Elements in List in Python

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

457 Views

When it is required to find the fractional frequency of elements in a list, a dictionary comprehension, a simple iteration and the ‘Counter’ method is used.ExampleBelow is a demonstration of the same −from collections import Counter my_list = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65] print("The list is :") print(my_list) my_num = {index : 0 for index in set(my_list)} my_denominator = Counter(my_list) my_result = [] for element in my_list: my_num[element] += 1 my_result.append(str(my_num[element]) + '/' + str(my_denominator[element])) print("The result is :") print(my_result)OutputThe ... Read More

Concatenate Tuple to Dictionary Key in Python

AmitDiwan
Updated on 08-Sep-2021 08:23:41

395 Views

When it is required to concatenate tuple to a dictionary key, a list comprehension and the ‘join’ attribute are used.ExampleBelow is a demonstration of the same −my_list = [(("pyt", "is", "best"), 10), (("pyt", "cool"), 1), (("pyt", "is", "fun"), 15)] print("The list is :") print(my_list) my_result = {} for sub_list in my_list: my_result[" ".join(sub_list[0])] = sub_list[1] print("The result is :") print(my_result)OutputThe list is : [(('pyt', 'is', 'best'), 10), (('pyt', 'cool'), 1), (('pyt', 'is', 'fun'), 15)] The result is : {'pyt is best': 10, 'pyt cool': 1, 'pyt is fun': 15}ExplanationA list of tuple is ... Read More

Restrict Element Frequency in List in Python

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

384 Views

When it is required to restrict elements frequency in a list, a simple iteration is used along with the ‘append’ method.ExampleBelow is a demonstration of the same −from collections import defaultdict my_list = [11, 14, 15, 14, 11, 14, 14, 15, 15, 16] print("The list is :") print(my_list) my_dict = {14 : 3, 11 : 1, 16 : 1, 15 : 2} print("The dictionary is :") print(my_dict) my_result = [] my_def_dict = defaultdict(int) for element in my_list: my_def_dict[element] += 1 if my_def_dict[element] > my_dict[element]: ... Read More

Group List Elements in a Matrix using Python

AmitDiwan
Updated on 08-Sep-2021 08:20:37

314 Views

When it is required to list elements grouping in a matrix, a simple iteration, the ‘pop’ method, list comprehension and ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_list = [[14, 62], [51, 23], [12, 62], [78, 87], [41, 14]] print("The list is :") print(my_list) check_list = [14, 12, 41, 62] print("The list is :") print(check_list) my_result = [] while my_list: sub_list_1 = my_list.pop() sub_list_2 = [element for element in check_list if element not in sub_list_1] try: ... Read More

Convert Tuple to List by Adding String After Every Element in Python

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

270 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

Cross Join Every Kth Segment in Python

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

239 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

Find Character Position of Kth Word from List of Strings in Python

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

363 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

Replace All Characters of a List Except the Given Character in Python

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

478 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

Concatenate Strings Around K in Python

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

190 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

Advertisements