accessing elements of a tuple

Matthew Sacks ntwrkd at gmail.com
Fri Jan 30 23:42:48 CET 2009


>First of all, list is a reserved word.  Don't use it as a variable name.
I was using it as an example in this case.

>mylist[0][1] if I understand the question.
This works. Thank you.

On Fri, Jan 30, 2009 at 2:39 PM, Tim Chase
<python.list at tim.thechases.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?
>>
>> 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