accessing elements of a tuple

D'Arcy J.M. Cain darcy at druid.net
Fri Jan 30 17:34:23 EST 2009


On Fri, 30 Jan 2009 14:23:31 -0800
Matthew Sacks <ntwrkd at gmail.com> wrote:
> let me re-phrase that question:
> i would like to access the element of individual tuples inside of a
> list, by using an index.
> so i have the list contents
> 
> print list
> [('--datasourcename', 'DB'), ('--password', '123')]
> 
> How can I access "DB" from the list directly using an index?

First of all, list is a reserved word.  Don't use it as a variable name.

mylist[0][1] if I understand the question.

I don't know what you are trying to accomplish but is this snippet
useful?

opts = dict([(x[0][2:], x[1]) for x in mylist if x[0][:2] == '--'])
print opts['datasourcename']

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list