Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

1. Meet Python: Everyone loves lists > Handle many levels of nested lists

Handle many levels of nested lists

The data and your code are not in sync.

The movie buff’s data is a list that contains a nested list that itself contains a nested list. The trouble is that your code knows only how to process a list nested inside an enclosing list.

The solution, of course, is to add more code to handle the additionally nested list. By looking at the existing code, it’s easy to spot the code you need to repeat:

image with no caption

An IDLE Session

Let’s use IDLE once more to test this latest iteration of your code:

image with no caption
image with no caption

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.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint