Given two lists, extract maximum of elements with similar K in corresponding list.
Input : test_list1 = [4, 3, 6, 2, 8], test_list2 = [3, 6, 3, 4, 3], K = 3
Output : 8
Explanation : Elements corresponding to 3 are, 4, 6, and 8, Max. is 8.
Input : test_list1 = [10, 3, 6, 2, 8], test_list2 = [5, 6, 5, 4, 5], K = 5
Output : 10
Explanation : Elements corresponding to 5 are, 10, 6, and 8, Max. is 10.
In this, we extract all elements from list 1 which are equal to K in list 2, and then perform max() to get maximum of them.
OutputThe original list 1 is : [4, 3, 6, 2, 9]
The original list 2 is : [3, 6, 3, 4, 3]
Extracted Maximum element : 9
In this, we perform task of pairing elements using zip() and is one-liner solution provided using list comprehension.
OutputThe original list 1 is : [4, 3, 6, 2, 9]
The original list 2 is : [3, 6, 3, 4, 3]
Extracted Maximum element : 9
1. Create a list of tuples where each tuple contains elements from both test_list1 and test_list2 at corresponding indices.
2. Create a new list using list comprehension to filter out tuples that have the element K in test_list2.
3. Use heapq.nlargest() function to return the K largest elements from the filtered list.
4. Return the first element from the resulting list.