Found 26504 Articles for Server Side Programming

Python – Cross Pattern Pairs in List

AmitDiwan
Updated on 08-Sep-2021 08:48:03

243 Views

When it is required to display cross pattern pairs in list, a list comprehension and the ‘*’ operator are used.Below is a demonstration of the same −Examplemy_list_1 = [14, 35, 26] my_list_2 = [36, 24, 12] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) result = [i * j for j in my_list_1 for i in my_list_2] print ("The result is :") print(result)OutputThe first list is : [14, 35, 26] The second list is : [36, 24, 12] The result is : [504, 336, 168, 1260, 840, 420, 936, 624, 312]ExplanationTwo lists are ... Read More

Python – Nearest occurrence between two elements in a List

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

287 Views

When it is required to display nearest occurrence between two elements in a list, a method is defined that takes three parameters. It uses the ‘not in’ operator, and the list comprehension to determine the result.Below is a demonstration of the same −Exampledef nearest_occurence_list(my_list, x, y):    if x not in my_list or y not in my_list:       return -1    x_index = [index for index in range(len(my_list)) if my_list[index] == x]    y_index = my_list.index(y)    min_dist = 1000000    result = None    for element in x_index:       if abs(element - y_index) < min_dist: ... Read More

Python – Filter Tuples with Integers

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

192 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

269 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

307 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

189 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

230 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

179 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

Advertisements