0

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.

Adam
  • 840
  • 6
  • 24
  • 1
    use the `{ }` button or `ctrl+k` to make the selected text into a *block of code* when editing. – Antti Haapala -- Слава Україні Sep 15 '17 at 21:05
  • 1
    And the term you're looking for is recursion. – Antti Haapala -- Слава Україні Sep 15 '17 at 21:06
  • 2
    And there is a question on the generic thing, i.e. flattening a list: https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists – Antti Haapala -- Слава Україні Sep 15 '17 at 21:08
  • 1
    Possible duplicate of [What is recursion and when should I use it?](https://stackoverflow.com/questions/3021/what-is-recursion-and-when-should-i-use-it) – Purag Sep 15 '17 at 21:46
  • I want to understand that –  Sep 15 '17 at 23:59
  • `print_lol()` is a **function**, and `print_lol(each_item)` is repeatedly calling **itself** (the function) with each new `each_item`, but original code has no function, so it **manually** and **explicitly** must handle every `each_item` individually. When the repeating behavior is put into a *function* it is a technique called **recursion** which means **"repeating a function"**, so each time the code encounters the same datatype, it repeats the function, which is what makes it more abbreviated than the original code. Does that make sense? Ask more questions if you still do not understand. – chickity china chinese chicken Sep 16 '17 at 00:14
  • Thank you @downshift for responding and explaining and might understand it –  Sep 18 '17 at 17:10
  • Very welcome, @MohamedKhaled, it may still be confusing, ask more questions if we can help explain more – chickity china chinese chicken Sep 18 '17 at 17:13
  • but in this code `def sum_number(number): if number == 0: return 0 else: return number + sum_number(number-1) print(sum_number(3)) `@downshift –  Sep 18 '17 at 18:14
  • The result is **6** but I'm trying to analyze the code but there is problem –  Sep 18 '17 at 18:17

0 Answers0