accessing elements of a tuple

John Machin sjmachin at lexicon.net
Fri Jan 30 19:19:19 EST 2009


On Jan 31, 9:42 am, Matthew Sacks <ntw... at gmail.com> wrote:
> >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.l... 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]

But that sort of code rapidly becomes unreadable. Unpacking the list
and unpacking the tuple into named components will be more
understandable when you revisit the code.



More information about the Python-list mailing list