0

I'm making a function where I have to find a the lowest value in a list of lists recursively. For example, a list containing [1, 2, 3, [4, 5]] would return 1.

I have some code written already. However, I don't know how to iterate through a list and compare the lowest number to a list.

I have included a comment where my function gets an error in my code below: def recMin(nestedLis):

lowest = 1000000

if len(nestedLis) < 2:
    return nestedLis
else:
    # This function works until it hits a list in a list (ex. [1, 2])
    # because it cannot compare a list to an int. I don't know how to fix this
    if nestedLis[0] < lowest:
        lowest = nestedLis[0]
        print(lowest)
    return lowest + recMin(nestedLis[1:])

Any help would be greatly appreciated!

Eileen
  • 3
  • 2

1 Answers1

0

I believe this does what you want.

import collections

def flatten(l):   # function copied from the link above
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el

new_list = list(flatten(lst))
print(max(new_list))

This was from this post: Highest and lowest value of nested lists using recursion