[Zope] Grabbing an item from a list of tuples

Thomas B. Passin tpassin@mitretek.org
Fri, 19 Jul 2002 11:54:38 -0400


[<abg@comco-inc.com>]

> I am writing a script which returns a list of tuples, like so:
>
> [('http://www.news.com/frontdoor/0-1.html?tag=hd_ts', 'CNET'),
> ('http://www.news.com/shopping/0-1257.html?tag=hd_ts', 'Price
comparisons')]
>
> My question (and it's a dumb one) is: how do I refer to the individual
> values within the tuples when iterating through the list?
>
> Here's what I've got so far:
>
> myCount = 0
>
> for elem in p.anchors:
> for elem in elem:
> print myCount, ": ", elem[0:]
> myCount = myCount + 1
>

count=0
for tuple in list_of_tuples:
    print count, tuple[1], ":", tuple[0]

    # Alternative print statement
    print "%s %s: %s" % (count,tuple[1],tuple[0])

    count=count+1

The syntax elem[0:] picks out all member from 0 to the end of the list or
tuple.  Also, it is asking for trouble to use the same variable name
("elem") in the inner and outer loops.

Cheers,

Tom P