Lists of lists and tuples, and finding things within them
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Nov 9 13:45:11 EST 2006
At Thursday 9/11/2006 15:06, 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 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' ]
You can even keep the original tuples, and create a dict when needed
(if searches like this are seldom used):
print dict(x)['host']
print dict(x).get('host')
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
More information about the Python-list
mailing list