Cleaner idiom for text processing?

Duncan Booth me at privacy.net
Wed May 26 03:50:07 EDT 2004


Peter Otten <__peter__ at web.de> wrote in news:c91got$vpi$00$1 at news.t-
online.com:

> Yet another way to create the dictionary:
> 
>>>> import itertools
>>>> nv = iter("foo 1 bar 2 baz 3\n".split())
>>>> dict(itertools.izip(nv, nv))
> {'baz': '3', 'foo': '1', 'bar': '2'}
>>>>

You can also do that without using itertools:

>>> nv = iter("foo 1 bar 2 baz 3\n".split())
>>> dict(zip(nv,nv))
{'baz': '3', 'foo': '1', 'bar': '2'}
>>> 

However, I'm not sure I trust either of these solutions. I know that 
intuitively it would seem that both zip and izip should act in this way, 
but is the order of consuming the inputs actually guaranteed anywhere?




More information about the Python-list mailing list