jasmine jasmine - 4 months ago 36
Python Question

How to find maximum number in a 2d python list

I have a list in python as

my_list = [2,4,6,[5,10,3]]

How can I find the maximum number (i.e the program should return the max as 10)?



Flatten your list, and then you can use the max() builtin function:

l = [2,4,6,[5,10,3]]

def flatten(seq):
  for el in seq:
    if isinstance(el, list):
      yield from flatten(el)
      yield el

print(max(list(flatten(l)))) # 10