This original code using repeated code
movies = ["hello", "take", ["near", "others",["tra", "told", "model"]]]
for each_item in movies:
if isinstance(each_item, list):
for nested_item in each_item:
if isinstance(nested_item, list):
for deeper_item in nested_item:
if isinstance(deeper_item, list):
for deepest_item in deeper_item:
print(deepest_item)
else:
print(deeper_item)
else:
print(nested_item)
else:
print(each_item)
This is without using a function. When I want to condense code by removing the repeated logic, the new code (using a function I call print_lol
) will be
movies = ["hello", "take", ["near", "others",["tra", "told", "model"]]]
def print_lol(the_list):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)
print_lol(movies)
I want to understand print_lol(each_item)
in the if
statement. What does it do? Using print_lol(each_item)
inside its own function definition made it repeat, but I don't understand how.