Making a dict from two lists/tuples
Carlos Ribeiro
cribeiro at mail.inet.com.br
Thu May 24 15:15:33 EDT 2001
At 06:51 24/05/01 -0400, Andreas Jung wrote:
>dict={}
>
>for i in range(len(keys)):
> dict[keys[i]] = values[i]
I think that this kind of construct has been beaten to the death many
times, but it keeps being used, mainly because it looks similar to the
'for' statement of C or Pascal.
This kind of loop should be avoided in Python for several reasons:
- Explicit for loops in Python are generally much slower than implicit
loops, such the ones provided by map(), zip() or list comprehensions. *In
this particular case* some of the Pythonic solutions still use 'for' loops
though; more on this later.
- Range(len(keys)) builds a list. This takes some time, and uses up a
potentially big amount of memory. This is not an issue in some cases;
however, if the body of the 'for' statement is comprised of simple
statements, the performance hit may be noticeable.
- The worse aspect is that you built an intermediate index list (the
range() one) that had nothing to do with the original problem. It's just an
artifact; it's not part of the problem space.
- There is also subtler reason to avoid it: it is expressed in a imperative
way; you are telling The Computer to do exactly what you want it do to step
by step. This low level approach is excellent if you are doing low level
stuff. However, this is the kind of approach that makes you lose the focus
on the higher levels of the problem. This leads to problems when trying to
propose more abstract solutions; you are so deep in implementation details
that the higher level abstractions become harder to understand.
Someone has written about this a few days ago. C/C++ programmers tend to
avoid 'complex' data structures such as mappings when prototyping. The
reason is that it is much harder to quickly write-compile-run a working
prototype of these high level structures in a low level language. You end
up having to write a lot of stuff just to make you prototype work. That's
when many people switch to Python :-)
Take a look at some of the solutions proposed by Duncan Booth in a previous
message:
1) for key, value in zip(keys,values): dict[key] = value
2) for key, value in map(None, keys,values): dict[key] = value
3) map(dict.setdefault, keys, values)
The first two ones still use a explicit 'for' loop. However, we avoid
creating a intermediate index list, that truly had nothing to do with the
original problem. Both the zip() or the map() calls are being used for the
same reason: to 'pair up' all elements of both lists.
The third option is the simplest one. It is written in a functional
fashion, and it is pretty simple: map all keys to a value, using the
setdefault call. My *only* remark here has to do with the 'setdefault'
method name, but that's another issue <wink>
Carlos Ribeiro
More information about the Python-list
mailing list