0

Let's say I have a list of lists.

>>> my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]

Is there a way to take all the items from that list and make it a single, un-nested list? Something like this:

>>> break_up(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

LogicalX
  • 95
  • 1
  • 10

1 Answers1

1

This uses recursion

def appnd(new_list = [], list_to_append=[]):
    for i in list_to_append:
        if type(i) == list:
            appnd(new_list=new_list, list_to_append=i)
        else:
            new_list.append(i)

def main():
    my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]
    new_list = []
    appnd(new_list=new_list, list_to_append=my_list)
    print(new_list)


if __name__ == '__main__':
    main()
DontBe3Greedy
  • 564
  • 5
  • 12