accessing elements of a tuple

Tim Chase python.list at
Fri Jan 30 23:39:39 CET 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


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.


More information about the Python-list mailing list