469,282 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Create dict from two lists

py
I have two lists which I want to use to create a dictionary. List x
would be the keys, and list y is the values.

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

Any suggestions? looking for an efficent simple way to do this...maybe
i am just having a brain fart...i feel like this is quit simple.

thanks.

Feb 10 '06 #1
9 24215
py wrote:
I have two lists which I want to use to create a dictionary. List x
would be the keys, and list y is the values.

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

Any suggestions? looking for an efficent simple way to do this...maybe
i am just having a brain fart...i feel like this is quit simple.


dict(zip(x,y))

Diez
Feb 10 '06 #2
"py" <co*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have two lists which I want to use to create a dictionary. List x
would be the keys, and list y is the values.

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

Any suggestions? looking for an efficent simple way to do this...maybe
i am just having a brain fart...i feel like this is quit simple.

thanks.


x = [1,2,3,4,5]
y = ['a','b','c','d','e']
dict(zip(x,y))

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

-- Paul
Feb 10 '06 #3
On Fri, 2006-02-10 at 08:51, py wrote:
I have two lists which I want to use to create a dictionary. List x
would be the keys, and list y is the values.

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

Any suggestions? looking for an efficent simple way to do this...maybe
i am just having a brain fart...i feel like this is quit simple.


d = dict(zip(x,y))

-Carsten
Feb 10 '06 #4
Diez B. Roggisch wrote:
py wrote:
I have two lists which I want to use to create a dictionary. List x
would be the keys, and list y is the values.

x = [1,2,3,4,5]
y = ['a','b','c','d','e']

Any suggestions? looking for an efficent simple way to do this...maybe
i am just having a brain fart...i feel like this is quit simple.


dict(zip(x,y))

Diez

I'd even suggest using izip (from itertools), it's often much faster
than zip (end result is the same).

Demo:
from itertools import izip
l1 = range(5000)
l2 = range(5000, 10000)
from timeit import Timer
t1 = Timer("dict(zip(l1, l2))", "from __main__ import l1, l2")
t2 = Timer("dict(izip(l1, l2))", "from __main__ import l1, l2, izip")
min(t1.repeat(5, 10000)) 17.989041903370406 min(t2.repeat(5, 10000)) 10.381146486494799


42% speed gain from using izip instead of zip (memory was not an issue
during the test, machine had 1.3Gb of available ram)
Feb 10 '06 #5
py
Thanks, itertools.izip and just zip work great. However, I should have
mentioned this, is that I need to keep the new dictionary sorted.

d = {1:'first', -5 : 'negative 5', 6:'six', 99:'ninety-nine',
3:'three'}
keys = d.keys()
keys.sort()
vals = map(d.get, keys)

At this point keys is sorted [-5, 1, 3, 6, 99] and vals is sorted
['negative 5', 'first', 'three', 'six', 'ninety-nine']

Using zip does not create the dictionary sorted in this order.
new_d = dict(zip(keys, vals))

How can I use the two lists, keys and vals to create a dictionary such
that the items keep their order?

Thanks.

Feb 10 '06 #6

py wrote:
Thanks, itertools.izip and just zip work great. However, I should have
mentioned this, is that I need to keep the new dictionary sorted.

d = {1:'first', -5 : 'negative 5', 6:'six', 99:'ninety-nine',
3:'three'}
keys = d.keys()
keys.sort()
vals = map(d.get, keys)

At this point keys is sorted [-5, 1, 3, 6, 99] and vals is sorted
['negative 5', 'first', 'three', 'six', 'ninety-nine']

Using zip does not create the dictionary sorted in this order.
new_d = dict(zip(keys, vals))

How can I use the two lists, keys and vals to create a dictionary such
that the items keep their order?

Thanks.


Short answer - you can't. Dictionaries aren't sequential structures,
so they have no sorted form. You can iterate through it in a sorted
manner however, as long as you keep your list of keys:

keys.sort()
for k in keys:
print d[k]

Iain

Feb 10 '06 #7
py wrote:
Thanks, itertools.izip and just zip work great. However, I should have
mentioned this, is that I need to keep the new dictionary sorted.


Dictionaries aren't sorted. Period.

/MiO
Feb 10 '06 #8
py:
Thanks, itertools.izip and just zip work great. However, I should have
mentioned this, is that I need to keep the new dictionary sorted.


A Python dictionary is an unsorted data structure, so you cannot have
it sorted as you please.
(I have seen that lot of people ask for a sorted dictionary and for
permutation/combination functions, maybe they are batteries to be
included too in the standard library.)
If you need an ordered dict you can manage it yourself, keeping the
lists of keys, etc, or you can use an ordered dict implementation:

http://www.voidspace.org.uk/python/odict.html
http://aspn.activestate.com/ASPN/Coo.../Recipe/438823
Etc.

Bye,
bearophile

Feb 10 '06 #9
py
Iain King wrote:
Short answer - you can't. Dictionaries aren't sequential structures,
so they have no sorted form. You can iterate through it in a sorted
manner however, as long as you keep your list of keys:

keys.sort()
for k in keys:
print d[k]

Iain


duh!....thanks.

Feb 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Wolfgang.Stoecher | last post: by
4 posts views Thread by Lorn | last post: by
7 posts views Thread by Chris Stout | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
3 posts views Thread by bruce | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.