Skip to content Skip to sidebar Skip to footer

Python Find Min & Max Of Two Lists

I have two lists such as: l_one = [2,5,7,9,3] l_two = [4,6,9,11,4] ...and I need to find the min and max value from both lists combined. That is, I want to generate a single min a

Solution 1:

Arguably the most readable way is

max(l_one + l_two)

or

min(l_one + l_two)

It will copy the lists, though, since l_one + l_two creates a new list. To avoid copying, you could do

max(max(l_one), max(l_two))
min(min(l_one), min(l_two))

Solution 2:

Another way that avoids copying the lists

>>>l_one = [2,5,7,9,3]>>>l_two = [4,6,9,11,4]>>>>>>from itertools import chain>>>max(chain(l_one, l_two))
11
>>>min(chain(l_one, l_two))
2

Solution 3:

You can combine them and then call min or max:

>>>l_one = [2,5,7,9,3]>>>l_two = [4,6,9,11,4]>>>min(l_one + l_two)
2
>>>max(l_one + l_two)
11

Solution 4:

if you just have lists like you do, this works, even with lists from different sizes :

min(min([i1,i2,i3]))

You may even have a smarter solution which works with different numpy array :

import numpy as np
i1=np.array(range(5))
i2=np.array(range(4))
i3=np.array(range(-5,5))
np.min(np.concatenate([i1,i2,i3]))

Solution 5:

If you want to select the maximum or minimum values from the two lists.I think the following will work:

from numpy importmaximumresult= maximum(l_one,l_two)

It will return a maximum value after comparing each element in this two lists.

Post a Comment for "Python Find Min & Max Of Two Lists"