Python - Maximum of K element in other list
Last Updated :
02 May, 2023
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))
OutputThe 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))
OutputThe 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))
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.
Similar Reads
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Maximum element in Cropped List Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Python - K Maximum elements with Index in List GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e
3 min read