Let’s use recursion to solve another problem involving lists:
We want to write a function that removes nested lists within a list but keeps the values contained.
nested_planets = ['mercury', 'venus', ['earth'], 'mars', [['jupiter', 'saturn']], 'uranus', ['neptune', 'pluto']] flatten(nested_planets) # ['mercury', # 'venus', # 'earth', # 'mars', # 'jupiter', # 'saturn', # 'uranus', # 'neptune', # 'pluto']
Remember our tools for recursive functions. We want to identify a base case, and we need to think about a recursive step that takes us closer to achieving the base case.
For this problem, we have two scenarios as we move through the list.
- The element in the list is a list itself.
- We have more work to do!
- The element in the list is not a list.
- All set!
Which is the base case and which is the recursive step?
flatten() which has a single parameter named
We’ll start by declaring a variable,
result and setting it to an empty list.
result is our intermediary variable that houses elements from
Returning an empty list isn’t much good to us, it should be filled with the values contained in
for loop to iterate through
Inside the loop, we need a conditional for our recursive step. Check if the element in the current iteration is a list.
We can use Python’s
isinstance() like so:
a_list = ['listing it up!'] not_a_list = 'string here' isinstance(a_list, list) # True isinstance(not_a_list, list) # False
For now, print
"List found!" in the conditional.
Outside of the method definition, call
flatten() and pass
planets as an argument.
We need to make the recursive step draw us closer to the base case, where every element is not a list.
After your print statement, declare the variable
flat_list, and assign it to a recursive call to
flatten() passing in your iterating variable as the argument.
flatten() will return a list, update
result so it now includes every element contained in
flatten() by calling it on the planets and printing the result.
Nice work! Now the base case.
If the iterating variable is not a list, we can update
result, so it includes this element at the end of the list.
flatten() should now return the complete result.
Print the result!