Re: Lists of lists and tuples, and finding things within them

Discussion in 'Python' started by Daniel Nogradi, Nov 9, 2006.

  1. > I have a program that keeps some of its data in a list of tuples.
    > Sometimes, I want to be able to find that data out of the list. Here is
    > the list in question:
    >
    > [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
    > ('character01', 'Thessalus')]
    >
    > For a regular list, I could do something like x.index('host') and find
    > the index of it, but I don't know how to do this for a tuple where the
    > data item isn't known in advance. For example, I want to get the "host"
    > entry from the list above; but I can only retrieve it if I know what it
    > contains (e.g., x.index(('host', 'dragonstone.org'))).
    >
    > Is there a better way to do this than a construct similar the following?
    >
    > for key, value in x:
    > if key == 'host':
    > print value
    >


    If I were you I would use a dictionary for such a thing:

    mydict = dict( password01='unk', host='dragonstone.org', port='1234',
    character01='Thessalus' )

    And then you would look up host by:

    mydict[ 'host' ]

    HTH,
    Daniel
    Daniel Nogradi, Nov 9, 2006
    #1
    1. Advertising

  2. Daniel Nogradi

    Guest

    <LameJoke>
    Thanks Captain Obvious!
    </LameJoke>
    Daniel Nogradi wrote:
    > > I have a program that keeps some of its data in a list of tuples.
    > > Sometimes, I want to be able to find that data out of the list. Here is
    > > the list in question:
    > >
    > > [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
    > > ('character01', 'Thessalus')]
    > >
    > > For a regular list, I could do something like x.index('host') and find
    > > the index of it, but I don't know how to do this for a tuple where the
    > > data item isn't known in advance. For example, I want to get the "host"
    > > entry from the list above; but I can only retrieve it if I know what it
    > > contains (e.g., x.index(('host', 'dragonstone.org'))).
    > >
    > > Is there a better way to do this than a construct similar the following?
    > >
    > > for key, value in x:
    > > if key == 'host':
    > > print value
    > >

    >
    > If I were you I would use a dictionary for such a thing:
    >
    > mydict = dict( password01='unk', host='dragonstone.org', port='1234',
    > character01='Thessalus' )
    >
    > And then you would look up host by:
    >
    > mydict[ 'host' ]
    >
    > HTH,
    > Daniel
    , Nov 9, 2006
    #2
    1. Advertising

  3. Daniel Nogradi

    Guest

    Daniel Nogradi wrote:
    > > I have a program that keeps some of its data in a list of tuples.
    > > Sometimes, I want to be able to find that data out of the list. Here is
    > > the list in question:
    > >
    > > [('password01', 'unk'), ('host', 'dragonstone.org'), ('port', '1234'),
    > > ('character01', 'Thessalus')]
    > >
    > > For a regular list, I could do something like x.index('host') and find
    > > the index of it, but I don't know how to do this for a tuple where the
    > > data item isn't known in advance. For example, I want to get the "host"
    > > entry from the list above; but I can only retrieve it if I know what it
    > > contains (e.g., x.index(('host', 'dragonstone.org'))).
    > >
    > > Is there a better way to do this than a construct similar the following?
    > >
    > > for key, value in x:
    > > if key == 'host':
    > > print value
    > >

    >
    > If I were you I would use a dictionary for such a thing:
    >


    (snipped)


    The sequence of tuples may have repeated "keys"; if
    that's the case, then you may use a filter/iterator instead.
    Using a dictionary would otherwise cause you to lose
    "values":

    import itertools

    mytuple = (('foo', 'bar'), ('foo', 'foobar'), ('baz', 'qux'))

    it = itertools.ifilter(lambda t : t[0] == 'foo', mytuple)

    for t in it:
    print t


    --
    Hope this helps,
    Steven
    , Nov 9, 2006
    #3
  4. On 9 Nov 2006 12:27:10 -0800, declaimed the
    following in comp.lang.python:

    > The sequence of tuples may have repeated "keys"; if
    > that's the case, then you may use a filter/iterator instead.
    > Using a dictionary would otherwise cause you to lose
    > "values":
    >

    Depends upon how the dictionary is created

    > mytuple = (('foo', 'bar'), ('foo', 'foobar'), ('baz', 'qux'))
    >

    adict = { "foo" : [ "bar", "foobar" ], "baz" : [ "qux"] }
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Nov 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  2. Replies:
    5
    Views:
    541
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    551
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    705
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    219
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page