Re: Create dict from two lists
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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/Cookbook/Python/Recipe/438823 Etc. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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, 1) >>> 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, 1)) 17.989041903370406 >>> min(t2.repeat(5, 1)) 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
"py" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Create dict from two lists
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 -- http://mail.python.org/mailman/listinfo/python-list