accessing elements of a tuple
Tim Chase
python.list at tim.thechases.com
Fri Jan 30 17:39:39 EST 2009
> 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?
>
> right now I would have to grab the tuple and the use the index of the tuple
Well, you can use
lst[0][1]
to get it. Or, if you're parsing through the list, you can use
tuple unpacking:
for name, value in lst:
print "%s = %s" % (name, value)
As an aside, it looks like you're doing parameter parsing. The
standard library has the optparse module which takes a lot of
pain out of parsing parameters.
-tkc
More information about the Python-list
mailing list