Reputation: 5
How can I make a function to return the lowest number in a list like:
listA = [[10,20,30],[40,50,60],[70,80,90]]
I want it to return 10
Upvotes: 0
Views: 50
Reputation: 79
This is possible using list comprehension
>>> listA = [[10,20,30],[40,50,60],[70,80,90]]
>>> output = [y for x in listA for y in x]
>>> min(output)
10
Upvotes: 0
Reputation: 133764
>>> listA = [[10,20,30],[40,50,60],[70,80,90]]
>>> min(y for x in listA for y in x)
10
Upvotes: 1
Reputation: 1049
Flatten the list by using itertools.chain
, then find the minimum as you would otherwise:
from itertools import chain
listA = [[10,20,30],[40,50,60],[70,80,90]]
min(chain.from_iterable(listA))
# 10
Upvotes: 2
Reputation: 2404
Set result
to float("inf")
. Iterate over every number in every list and call each number i
. If i
is less than result
, result = i
. Once you're done, result
will contain the lowest value.
Upvotes: 0