Parsing lists

Matthew Schinckel matt at null.net
Sun Jul 9 19:46:55 EDT 2000


On Jul 9, I overheard Seamus Venasse mutter:

> >>> list = [ 'title1', 'url1', 'title2', 'url2', 'title3', 'url3' ]
> >>> for title, url in list:
> ...     print title, url
> ...
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ValueError: unpack sequence of wrong size

You are trying to force each list item to be a 2-tuple

Try one of two methods:

list = [('title1','url1'), ('title2','url2'), ('title3','url3')]
for title,url in list:
    print title
    print url

or better:

dict = {'title1':'url1','title2':'url2','title3':'url3'}
for title in dict.keys():
    print title
    print dict[title]

-- 
Matthew Schinckel     <matt at null.net>

Expert, n.:
	Someone who comes from out of town and shows slides.




More information about the Python-list mailing list