I have three lists of different lengths.
For example
List1 is of length 40
List2 is of length 42
List3 is of length 47
How can I use the Python inbuilt min()
or any other method to find the list with the minimum length?
I tried:
min(len([List1,List2,List3]))
but I get TypeError: 'int' object is not iterable
You need to apply len()
to each list separately:
shortest_length = min(len(List1), len(List2), len(List3))
If you already have a sequence of the lists, you could use the map()
function or a generator expression:
list_of_lists = [List1, List2, List3]
shortest_length = min(map(len, list_of_lists)) # map function
shortest_length = min(len(l) for l in list_of_lists) # generator expr
To find the shortest list, not the shortest length, use the key
argument:
list_of_lists = [List1, List2, List3]
shortest_list = min(list_of_lists, key=len)
Use generator expression
min(len(i) for i in [List1,List2,List3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With