Page 54 -
P. 54

lists within lists


           Store lists within lists


          As you’ve seen, lists can hold data of mixed type. But it gets even better than
           that: lists can hold collections of anything, including other lists. Simply embed
           the inner list within the enclosing list as needed.
                                                                                          There’s only one lead
           Looking closely at the movie buff’s data, it is possible to determine a structure   actor listed here, but
           which looks much like a list of lists:                                         there could be more.

           There’s a list of movie facts…
                                      The Holy Grail, 1975, T erry Jones & T erry Gilliam, 91 mins
               …which itself contains        Graham Chapman
               a list of lead actors…
                                                    Michael Palin, John Cleese, T erry Gilliam, Eric Idle & T erry Jones
                     …which itself
                     contains a list of
                     supporting actors.


           In Python, you can turn this real list of data into code with little or no effort.
          All you need to remember is that every list is a collection of items separated
           from each other with commas and surrounded with square brackets. And, of
           course, any list item can itself be another list:
           The start of the
            first, outer list

                                                                                           The end of all the
                  movies = [                                                               lists is here.
                     "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
                         ["Graham Chapman",
                            ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

         The start of the
         second, inner list:       The start of the third, inner             This looks a little weird…until you
        “movies[4]”                inner list: “movies[4][1]”                remember that there are three
                                                                             opening square brackets, so there must
           So, a list within a list is possible, as is a list within a list within a list (as this   also be three closing ones.
           example code demonstrates). In fact, it’s possible to nest lists within lists to
           most any level with Python. And you can manipulate every list with its own list
           methods and access it with the square bracket notation:



                print(movies[4][1][3])                     Eric Idle

                           A list within a list within a list          Eric is this deeply nested, so he

                                                                       can’t possibly be idle. §
           18    Chapter 1
   49   50   51   52   53   54   55   56   57   58   59