[Zope] Grabbing an item from a list of tuples

Casey Duncan casey@zope.com
Fri, 19 Jul 2002 11:44:15 -0400


=46rom the looks of things you have a data structure like:

[(url, title), (url,title), ...]

If so then use Python's nifty unpacking feature:

for url, title in p.anchors:
    ..do something with url and title

hth,

-Casey

On Friday 19 July 2002 11:33 am, abg@comco-inc.com wrote:
> I am writing a script which returns a list of tuples, like so:
>=20
> [('http://www.news.com/frontdoor/0-1.html?tag=3Dhd_ts', 'CNET'),
> ('http://www.news.com/shopping/0-1257.html?tag=3Dhd_ts', 'Price compari=
sons')]
>=20
> 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?
>=20
> Here's what I've got so far:
>=20
> myCount =3D 0
>=20
> for elem in p.anchors:
> =09for elem in elem:
> =09=09print myCount, ": ", elem[0:]
> =09=09myCount =3D myCount + 1
>=20
> Here's what it returns:
>=20
> 0 :  http://www.news.com/frontdoor/0-1.html?tag=3Dhd_ts
> 1 :  CNET
> 2 :  http://www.news.com/shopping/0-1257.html?tag=3Dhd_ts
> 3 :  Price comparisons
>=20
> How can I specifically reference the urls/titles separately?
>=20
> Am I making sense?
>=20
> Thanks,
>=20
> Aaron Gillette
> abg@comco-inc.com
>=20
>=20
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -=20
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>=20