Page 61 -
P. 61
meet python
Let’s use IDLE once more to test this latest iteration of your code: Process a deeply nested
>>> for each_item in movies: list inside a nested list
if isinstance(each_item, list): inside an enclosing list.
for nested_item in each_item:
if isinstance(nested_item, list):
for deeper_item in nested_item:
print(deeper_item)
else:
print(nested_item)
else:
print(each_item)
The Holy Grail
1975 It works! This time, you
Terry Jones & Terry Gilliam see all of your list data
91 on screen.
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
I just love that...in fact, I love it so
much I’ve decided to add another list to my
data. I want to include the other movies each
supporting actor has starred in. If I add the
data, can you change your code to print this
data, too?
That’s more list data and more Python code.
The data has to be embedded as another nested list within the already deeply
nested list of supporting actors. That’s possible to do, even though it makes
your head hurt just to think about a list of lists of lists of lists! Amending your
code is just a matter of adding another for loop and an if statement.
That doesn’t sound like too much trouble, does it?
you are here 4 25