0

Given a list like the next one:

foo_list = [[1,8],[2,7],[3,6]]

I've found in questions like Tuple pairs, finding minimum using python and minimum of list of lists that the pair with the minimum value of a list of lists can be found using a generator like:

min(x for x in foo_list)

which returns

[1, 8]

But I was wondering if there is a similar way to return both minimum values of the "columns" of the list:

output = [1,6]

I know this can be achieved using numpy arrays:

output = np.min(np.array(foo_list), axis=0)

But I'm interested in finding such a way of doing so with generators (if possible).

Thanks in advance!

Javier TG
  • 465
  • 2
  • 7
  • 15

2 Answers2

3
[min(l) for l in zip(*foo_list)]

returns [1, 6]

zip(*foo_list) gets the list transpose and then we find the minimum in both lists. Thanks @mousetail for suggestion.

mcgusty
  • 1,354
  • 15
  • 21
1

You can use two min() for this. Like -

min1 = min(a for a, _ in foo_list)
min2 = min(b for _, b in foo_list)
print([min1, min2])

Will this do? But I think if you don't want to use third party library, you can just use plain old loop which will be more efficient.

kuro
  • 3,214
  • 3
  • 15
  • 31
  • Thanks, I've thought about it, but I was wondering if this can be achieved in one line (I may be over-thinking it) – Javier TG May 06 '21 at 11:16