Nested List question

Andrew Farrell amfarrell at mit.edu
Wed Feb 24 16:11:35 EST 2016


you can indeed have a nested list. One way you could do that looks like

donor = [
    'George',
    'Soros',
    [                              #<- 2nd element of outermost list
        '99 First Street',
        [                          #<- 1st element of middling list
          '33 Broadway Avenue',
          'Apt 303',               #<- 1st element of innermost list
        ],
        '1 Park Avenue'
    ],
    'New York', 'NY',
]
Then, to extract "Apt 303", you could do
donor[2][1][1]

A better explaination of nested lists can be found if you ctrl-f search
for "nested list" on this chapter
<http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap08.html> of
the book How to Think Like a Computer Scientist with python.

On Wed, Feb 24, 2016 at 2:59 PM, <grsmith at atlanticbb.net> wrote:

> All,
>
> Can you have a phython list like:
> ['George',
> 'Soros',
> ['99 First Street',
>   '33 Broadway Avenue', ['Apt 303'],
>   '1 Park Avenue'],
>   'New York', 'NY']
>
> In other words how do you correctly nest the
> ['Apt 303'] so it goes with 33 Broadway Avenue.
>
> Also, I tried several ways and could not figure out
> how to get the ['Apt 303'] out of the list. How can
> you do that. It is the ['Apt 303'] that is the problem,
> I know how to do the other elements.
>
> thanks
> George
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list