Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103463

Re: Nested List question

From Andrew Farrell <amfarrell@mit.edu>
Newsgroups comp.lang.python
Subject Re: Nested List question
Date 2016-02-24 15:11 -0600
Message-ID <mailman.106.1456348637.20994.python-list@python.org> (permalink)
References <4AD2810BE3B346399F777EDEAB079BDA@OPTIPLEX990>

Show all headers | View raw


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@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
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Nested List question Andrew Farrell <amfarrell@mit.edu> - 2016-02-24 15:11 -0600

csiph-web