I have a new flatten function, I use this in one of my programs because I get a list of tuples which In then add to a list the end result being a list of a list of tuples.
In needed to preserve the tuples I have to write a recursive function that would endlessly search for lists in the container objects, So this function I have here will flatten any data that contains a list as the topmost item, because if you have a dict of list of tuples you will want to preserve that.
With out further adieu here is my new flatten function.
def flatten(a_list):
"""
Flattens the given list of an arbitrary amount of lists/items into a single list with all values
:param a_list: Pass the list object you wish to flatten, no list will be left with in. Also maintains order.
:return: Returns this function with the hold list as an argument if there
are any list objects, else it returns the held list.
"""
hold = []
for item in a_list:
if list == type(item):
for it in item:
hold.append(it)
else:
hold.append(item)
return flatten(hold) if any(type(part) == list for part in hold) else hold