Dictionary from a list

EK ekayxu at gmail.com
Sat Aug 22 12:03:27 CEST 2009


On Aug 20, 2:10 pm, Peter Otten <__pete... at web.de> wrote:
> Jan Kaliszewski wrote:
> > 20-08-2009 o 02:05:57 Jan Kaliszewski <z... at chopin.edu.pl> wrote:
>
> >> Or probably better:
>
> >>      from itertools import islice, izip
> >>      dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))
>
> > Or similarly, perhaps more readable:
>
> >      iterator = iter(li)
> >      dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))
>
> I just can't stop posting this one:
>
> >>> from itertools import izip
> >>> it = iter([1,2,3,4,5,6])
> >>> dict(izip(it, it))
>
> {1: 2, 3: 4, 5: 6}
>
> I really tried, but yours drove me over the edge.
>
> Peter

dict(zip(*[iter(l)]*2))



More information about the Python-list mailing list