Found 10476 Articles for Python

Python – Extract Kth element of every Nth tuple in List

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

308 Views

When it is required to extract ‘K’th element of every ‘N’th tuple in a list, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [(54, 51, 23), (73, 24, 47), (24, 33, 72), (64, 27, 18), (63, 24, 67), (12, 25, 77), (31, 39, 80), (33, 55, 78)] print("The list is :") print(my_list) K = 1 print("The value of K is :") print(K) N = 3 print("The value of N is :") print(N) my_result = [] for index in range(0, len(my_list), N): my_result.append(my_list[index][K]) ... Read More

Python – Ordered tuples extraction

AmitDiwan
Updated on 08-Sep-2021 08:30:32

190 Views

When it is required to extract ordered tuples, a list comprehension, the ‘sorted’ method, the ‘tuple’ method and the ‘==’ operator is used.ExampleBelow is a demonstration of the same −my_list = [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] print("The list is :") print(my_list) my_result = [element for element in my_list if tuple(sorted(element)) == element] print("The result is :") print(my_result)OutputThe list is : [(15, 74, 36, 22, 54), (13, 24, 56), (59, 60, 34), (42, 65, 56), (99, 91)] The result is : [(13, 24, 56)]ExplanationA list of integers ... Read More

Python – Concatenate Rear elements in Tuple List

AmitDiwan
Updated on 08-Sep-2021 08:29:06

231 Views

When it is required to concatenate the rear elements in a tuple list, a list comprehension and the ‘join’ methods are used.ExampleBelow is a demonstration of the same −my_tuple = [(13, 42, "Will"), (48, "is a"), ("good boy", )] print("The tuple is : " ) print(my_tuple) my_result = " ".join([sub[-1] for sub in my_tuple]) print("The result is : " ) print(my_result)OutputThe list is : [(13, 42, 'Will'), (48, 'is a'), ('good boy', )] The concatenated list is : Will is a good boyExplanationA list of tuple is defined and is displayed on the console.A list comprehension and ... Read More

Python – Find Kth Even Element

AmitDiwan
Updated on 08-Sep-2021 08:28:08

183 Views

When it is required to find the ‘K’th even element in a list, a list comprehension and the ‘%’ operator is used.ExampleBelow is a demonstration of the same −my_list = [14, 63, 28, 32, 18, 99, 13, 61] print("The list is :") print(my_list) K = 3 print("The value of K is :") print(K) my_result = [element for element in my_list if element % 2 == 0][K] print("The result is :") print(my_result)OutputThe list is : [14, 63, 28, 32, 18, 99, 13, 61] The value of K is : 3 The result is : 18ExplanationA list of ... Read More

Python – Split List on next larger value

AmitDiwan
Updated on 08-Sep-2021 08:27:19

180 Views

When it is required to split a list based on the next larger value, a list comprehension, the ‘iter’ method and the ‘islice’ methods are used.ExampleBelow is a demonstration of the same −from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87, 11] print("The list is :") print(my_list) length_to_split = [2, 5, 3] print("The split length list is :") print(length_to_split) temp = iter(my_list) my_result = [list(islice(temp, element)) for element in length_to_split] print("The result is :") print(my_result)OutputThe list is : [11, 22, 33, 34, 45, 26, 87, 11] The split length list is : ... Read More

Python – Extract elements from Ranges in List

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

578 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

Python – Fractional Frequency of elements in List

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

446 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

Python – Concatenate Tuple to Dictionary Key

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

385 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

Python – Restrict Elements Frequency in List

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

378 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

Python – List Elements Grouping in Matrix

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

306 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

Advertisements