How to flatten a complex nested lists in Python

Today I will try to demonstrate how to flatten a list using Python.

Say, you have a complex nested list,

nested_list = [1, 2, [3, 4, [5],['hello world']], [6, [[[7, 'Python']]]]]

We shall use Python generator support for this,

def flat_the_list(a_list):
    # iterate through a_list
    for i in a_list:
    # check if it is instance of a list
        if isinstance(i, (list)):
            # loop through it
            for x in flat_the_list(i):
            yield x
        else:
            yield i

# test it

list(flat_the_list(nested_list))

[1, 2, 3, 4, 5, ‘hello world’, 6, 7, ‘Python’]