473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

opposite of dict.items()

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

TIA
Tertius

Jul 18 '05 #1
6 2406
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
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
|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
[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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
7
by: Marcio Rosa da Silva | last post by:
Hi! In dictionaries, unlinke lists, it doesn't matter the order one inserts the contents, elements are stored using its own rules. Ex: >>> d = {3: 4, 1: 2} >>> d {1: 2, 3: 4}
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
15
by: Jay Tee | last post by:
Hi, I have some code that does, essentially, the following: - gather information on tens of thousands of items (in this case, jobs running on a compute cluster) - store the information as a...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
3
by: Phoe6 | last post by:
I have a requirement for using caseless dict. I searched the web for many different implementations and found one snippet which was implemented in minimal and useful way. ############# import...
10
by: Gerard flanagan | last post by:
Simon Mullis wrote: data = from itertools import groupby datadict = \ dict((k, len(list(g))) for k,g in groupby(data, lambda s: s)) print datadict
6
by: Ernst-Ludwig Brust | last post by:
Given 2 Number-Lists say l0 and l1, count the various positiv differences between the 2 lists the following part works: dif= da={} for d in dif: da=da.get(d,0)+1 i wonder, if there is a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.