Open In App

Python - Maximum of K element in other list

Last Updated : 02 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 
 

Method #1 : Using loop + max() 

 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.

Python3
# Python3 code to demonstrate working of 
# Maximum of K element in other list
# Using loop + max()

# initializing lists
test_list1 = [4, 3, 6, 2, 9]
test_list2 = [3, 6, 3, 4, 3]

# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))

# initializing K 
K = 3

res = []
for idx in range(len(test_list1)):
    
    # checking for K in 2nd list
    if test_list2[idx] == K :
        res.append(test_list1[idx])

# getting Maximum element
res = max(res)

# printing result 
print("Extracted Maximum element : " + str(res))

Output
The original list 1 is : [4, 3, 6, 2, 9]
The original list 2 is : [3, 6, 3, 4, 3]
Extracted Maximum element : 9

Method #2 : list comprehension + max() + zip()

In this, we perform task of pairing elements using zip() and is one-liner solution provided using list comprehension.

Python3
# Python3 code to demonstrate working of 
# Maximum of K element in other list
# Using list comprehension + max() + zip()

# initializing lists
test_list1 = [4, 3, 6, 2, 9]
test_list2 = [3, 6, 3, 4, 3]

# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))

# initializing K 
K = 3

# one liner to solve this problem
res = max([sub1 for sub1, sub2 in zip(test_list1, test_list2) if sub2 == K])

# printing result 
print("Extracted Maximum element : " + str(res))

Output
The original list 1 is : [4, 3, 6, 2, 9]
The original list 2 is : [3, 6, 3, 4, 3]
Extracted Maximum element : 9

Method #3 : Using heapq

Approach

Using heapq.nlargest() to find the max number 

Algorithm

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.

Python3
import heapq

def max_k_element_3(test_list1, test_list2, K):
    zipped_list = list(zip(test_list1, test_list2))
    filtered_list = [x[0] for x in zipped_list if x[1] == K]
    return heapq.nlargest(K, filtered_list)[0]
test_list1 = [4, 3, 6, 2, 9]
test_list2 = [3, 6, 3, 4, 3]
K=3
print(max_k_element_3(test_list1, test_list2, K))

Output
9

Time complexity: O(n log K) - due to using heapq.nlargest() function
Space complexity: O(n) - for creating a list of tuples

Method #4: Using numpy

Step-by-step approach:

  • Import the numpy library.
  • Convert both lists into numpy arrays using the np.array() method.
  • Use the np.where() method to find the indices of elements in test_list2 that are equal to K.
  • Use the resulting indices to slice the corresponding elements in test_list1 and store them in a new numpy array.
  • Use the np.amax() method to find the maximum value in the new array.
  • Convert the result back to a regular Python integer using the int() method.
  • Print the final result.
Python3
import numpy as np

# initializing lists
test_list1 = [4, 3, 6, 2, 9]
test_list2 = [3, 6, 3, 4, 3]

# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))

# initializing K 
K = 3

# convert lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)

# find indices where arr2 == K
indices = np.where(arr2 == K)[0]

# slice arr1 using indices and store in new array
new_arr = arr1[indices]

# find maximum value in new array
max_val = np.amax(new_arr)

# convert result to integer
res = int(max_val)

# printing result 
print("Extracted Maximum element : " + str(res))
OUTPUT :
The original list 1 is : [4, 3, 6, 2, 9]
The original list 2 is : [3, 6, 3, 4, 3]
Extracted Maximum element : 9

Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), for the numpy arrays and other variables created in the process.


Next Article
Practice Tags :

Similar Reads