Dictionary from a list

I

iu2

Hi all,

I need to create a dictionary out of a list.

Given the list [1, 2, 3, 4, 5, 6]

I need the dictionary: {1:2, 3:4, 5:6}

I'll appreciate your help
Thanks
iu2
 
D

Diez B. Roggisch

iu2 said:
Hi all,

I need to create a dictionary out of a list.

Given the list [1, 2, 3, 4, 5, 6]

I need the dictionary: {1:2, 3:4, 5:6}

dict(zip(l[::2], l[1::2]))

Diez
 
J

Jan Kaliszewski

19-08-2009 o 22:52:54 iu2 said:
iu2 said:
I need to create a dictionary out of a list.
Given the list [1, 2, 3, 4, 5, 6]
I need the dictionary: {1:2, 3:4, 5:6}

dict(zip(l[::2], l[1::2]))

Or (for long lists, when memory becomes expensive):

dict(li[i:i+2] for i in xrange(0, len(li), 2))

Or probably better:

from itertools import islice, izip
dict(izip(islice(li, 0, None, 2), islice(li, 1, None, 2)))

Cheers,
*j
 
J

Jan Kaliszewski

20-08-2009 o 02:05:57 Jan Kaliszewski said:
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))
 
S

Steven D'Aprano

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.

If you want something to drive you over the edge:

alist = [1, 2, 3, 4, 5, 6]
dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))
{1: 2, 3: 4, 5: 6}


Enjoy :)
 
P

Peter Otten

Steven said:
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.

If you want something to drive you over the edge:

I meant that figuratively...
alist = [1, 2, 3, 4, 5, 6]
dict(apply(zip, map(lambda n: map(lambda t: t[1], filter(lambda t:
((not (t[0]%2)) == 1) == n, enumerate(alist))), range(1, -1, -1))))
{1: 2, 3: 4, 5: 6}
....originally.

Enjoy :)

Not ;)
 
I

iu2

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

Nice.
(but looks like stepping towards the dark side ... :)

I also liked this one:
iterator = iter(li)
dict((iterator.next(), iterator.next()) for i in xrange(len(li)/2))

which inspired me to do something quite similar:

a=range(1, 11)
a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
dict([[a.pop(0), a.pop(0)] for i in range(len(a)/2)])
{1: 2, 3: 4, 9: 10, 5: 6, 7: 8}


Thanks
 
A

Aahz

from itertools import izip
it =3D iter([1,2,3,4,5,6])
dict(izip(it, it))

{1: 2, 3: 4, 5: 6}

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

No, that's not a good solution. For starters, it's less readable than
Peter's version. More importantly, it has poor memory use because it
needs to construct both the intermediate tuple and the zip() list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top