Found 33676 Articles for Programming

Python – Filter Tuples with Integers

AmitDiwan
Updated on 08-Sep-2021 08:34:14

195 Views

When it is required to filter tuple with integers, a simple iteration and the ‘not’ operator and the ‘isinstance’ method is used.ExampleBelow is a demonstration of the same −my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): temp = False break ... Read More

Python – Extract tuples with elements in Range

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

272 Views

When it is required to extract tuples with elements in a given range, the filter and lambda methods are used.ExampleBelow is a demonstration of the same −my_list = [(13, 15, 17), (25, 56), (13, 21, 19 ), (44, 14)] print("The list is :") print(my_list) beg, end = 13, 22 my_result = list(filter(lambda sub : all(element >= beg and element

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

232 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

184 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

183 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

580 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

386 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

Advertisements