By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

opposite of dict.items()

P: n/a
Is there a method to create a dict from a list of keys and a list of
values ?

TIA
Tertius

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tertius wrote:
Is there a method to create a dict from a list of keys and a list of
values ?

dict(zip(range(3), "abc"))

{0: 'a', 1: 'b', 2: 'c'}

Not a method() but a method.

Peter
Jul 18 '05 #2

P: n/a
Peter Otten wrote:
Tertius wrote:

Is there a method to create a dict from a list of keys and a list of
values ?


dict(zip(range(3), "abc"))


{0: 'a', 1: 'b', 2: 'c'}

Not a method() but a method.

Peter


much better than a for loop :)
thnx

Jul 18 '05 #3

P: n/a
|a = {}
|for k , v in zip(keys,values):
| a[k] = v
keys = (1,2,3,4)
vals = 'a b c d'.split()
dct = dict(zip(keys,vals))
dct

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

--
mertz@ | The specter of free information is haunting the `Net! All the
gnosis | powers of IP- and crypto-tyranny have entered into an unholy
..cx | alliance...ideas have nothing to lose but their chains. Unite
| against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------
Jul 18 '05 #4

P: n/a
[Tertius wrote]
Is there a method to create a dict from a list of keys and a list of
values ?
[Peter Otten]
dict(zip(range(3), "abc")) {0: 'a', 1: 'b', 2: 'c'}

If you're using Py2.3, then the itertools way is a bit nicer:
dict(itertools.izip(range(3), "abc"))

{0: 'a', 1: 'b', 2: 'c'}
Raymond Hettinger
Jul 18 '05 #5

P: n/a
Tertius <tc****@ananzi.co.za> writes:
Tertius wrote:
Is there a method to create a dict from a list of keys and a list of
values ?
TIA
Tertius

I found a way...

a = {}
for k , v in zip(keys,values):
a[k] = v


Or:
python

Python 2.3 (#1, Jul 30 2003, 21:59:29)
[GCC 3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
a=(1,2,3)
b=(4,5,6)
dict(zip(a,b)) {1: 4, 2: 5, 3: 6}


Greetings
Berthold
--
bh***@web.de / http://starship.python.net/crew/bhoel/
It is unlawful to use this email address for unsolicited ads
(USC Title 47 Sec.227). I will assess a US$500 charge for
reviewing and deleting each unsolicited ad.
Jul 18 '05 #6

P: n/a
On Fri, 2003-08-29 at 04:53, Peter Otten wrote:
dict(izip(irange(3), "abc"))
{0: 'a', 1: 'b', 2: 'c'}

If I could use the above in Py2.4, it would be even nicer, namely:

- convert itertools to builtins


The push is to have LESS builtins. You can always import them in to the
current namespace:

from itertools import izip
- add irange(), perhaps as an alias for xrange() like file/open


I suggested an irange to Raymond, and even coded one up, a while ago.
He sees it as bloat, and rightfully so, since there is a push to allow
optimizations of range that would achieve the same effect. (ie. rather
than remembering when to use range(), xrange(), or irange(), we could
just always use range() and the language would do lazy evaluation
whenever possible.) Unless that avenue turns out to be a complete dead
end, don't expect an irange().

BTW. In the example you used, you COULD use xrange(). The only problem
is that range has been extended, in 2.3, to accept longs, and xrange()
only works with ints, and Guido wants it that way (he doesn't want to
extend xrange()'s features, since it is a pain.) irange(), if it ever
appears, would presumably be fully range() compatible.

--
Chad Netzer
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.