One week before I was trying write an iterative generator, to flattening deep nested lists. The final result was this: https://stackoverflow.com/questions/2158...1#45213701
But I started with a recursive generator, which was much easier.
You can take this generator and just use min(flat(your_list)).
The yield from syntax makes it much easier to write generators, which are iterating over inalterables and/or generators.
Your example does too much. I think it's better to sort the functionality for maxnum out and do it outside of the function:
But I started with a recursive generator, which was much easier.
from collections import Iterable def flat(iterable): for element in iterable: if isinstance(element, Iterable) and not isinstance(element, (str, bytes)): yield from flat(element) else: yield elementIt's a more general approach. To convert this generator into an iterative generator was a bit confusing.
You can take this generator and just use min(flat(your_list)).
The yield from syntax makes it much easier to write generators, which are iterating over inalterables and/or generators.
Your example does too much. I think it's better to sort the functionality for maxnum out and do it outside of the function:
def flat_list_func(iterable): return_list = [] # your new flat list for element in iterable: if isinstance(element, (list,tuple)): return_list.extend(flat_list_func(element)) # here you've forgotten to return your max_val # I'm extending the return_list, which elements comes # from the recursive function call else: # if element is not list or tuple, it's appended to the # return_list return_list.append(element) return return_list nested_list = (5, (1,2), [[1],[9]]) flat_list = flat_list_func(nested_list) min_val = min(flat_list) max_val = max(flat_list) import statistics mean_val = statistics.mean(flat_list) median_val = statistics.median(flat_list) # and much moreIn general it's often easier writing recursive functions as writing the iterative counter part. In most languages we have the problem, that every function gets an own stack. If you have 32.000 functions running because of recursion, it's consuming memory. In Python the recursion depth is limited to 1000. You even have this problem in C.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!