473,387 Members | 1,859 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,387 software developers and data experts.

Multiplying all the values in a dictionary

Say I have a dictionary like below:

d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}

Say I want to multiply all the values of the dictionary by 2:

for key in d.keys():
d[key] = map(lambda x: x*2, d.get(key))

Is there a better/faster/cleaner way to achieve this ?

Thanks,

John
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Mar 24 '06 #1
2 8513
John McMonagle wrote:
Say I have a dictionary like below:

d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}

Say I want to multiply all the values of the dictionary by 2:
for key in d.keys():
d[key] = map(lambda x: x*2, d.get(key))
Is there a better/faster/cleaner way to achieve this ?


To update, I'd either go with that or Felipe's list comprehension.
If you just want the value of the result, how about:

doubled = dict((key, [x * 2 for x in values])
for key, values in d.iteritems())

--
-Scott David Daniels
sc***********@acm.org
Mar 24 '06 #2
Em Qui, 2006-03-23 Ã*s 17:54 -0800, Scott David Daniels escreveu:
John McMonagle wrote:
Say I have a dictionary like below:

d = {(100,500):[5,5], (100,501):[6,6], (100,502):[7,7]}

Say I want to multiply all the values of the dictionary by 2:
for key in d.keys():
d[key] = map(lambda x: x*2, d.get(key))
Is there a better/faster/cleaner way to achieve this ?


To update, I'd either go with that or Felipe's list comprehension.
If you just want the value of the result, how about:

doubled = dict((key, [x * 2 for x in values])
for key, values in d.iteritems())


And always benchmark:

$ python2.4 -mtimeit -s 'd = {(100,500):[5,5], (100,501):[6,6],
(100,502):[7,7]}; x = dict(d)' 'for key in d.keys(): x[key] = map(lambda
x: x*2, d.get(key))'
100000 loops, best of 3: 8.48 usec per loop

$ python2.4 -mtimeit -s 'd = {(100,500):[5,5], (100,501):[6,6],
(100,502):[7,7]}; x = dict(d)' 'for key in d: x[key] = [y*2 for y in
d[key]]' 100000 loops, best of 3: 3.92 usec per loop

You get a large improvement here because:
- lambdas are slow compared to direct operations:
$ python2.4 -mtimeit -s 'a = lambda x: x*2' 'a(1)'
1000000 loops, best of 3: 0.411 usec per loop
$ python2.4 -mtimeit -s 'def a(x): return x*2' 'a(1)'
1000000 loops, best of 3: 0.41 usec per loop
$ python2.4 -mtimeit '1*2'
10000000 loops, best of 3: 0.16 usec per loop
- d.get(x) is slower than d[x]:
$ python2.4 -mtimeit -s 'd = {1:1}' 'd[1]'
10000000 loops, best of 3: 0.172 usec per loop
$ python2.4 -mtimeit -s 'd = {1:1}' 'd.get(1)'
1000000 loops, best of 3: 0.363 usec per loop
- "for key in d:" is faster than "for key in d.keys():":
$ python2.4 -mtimeit -s 'd = {1:1}' 'for k in d: pass'
1000000 loops, best of 3: 0.345 usec per loop
$ python2.4 -mtimeit -s 'd = {1:1}' 'for k in d.keys(): pass'
1000000 loops, best of 3: 0.69 usec per loop

$ python2.4 -mtimeit -s 'd = {(100,500):[5,5], (100,501):[6,6],
(100,502):[7,7]}; x = dict(d)' 'dict((key, [x * 2 for x in values]) for
key, values in d.iteritems())'
100000 loops, best of 3: 7.72 usec per loop

Here it's slower because it creates another dictionary. But it's a
different result, can't be compared directly.

HTH,

--
Felipe.

Mar 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
2
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
3
by: mwt | last post by:
I want to set default values for a ConfigParser. So far, its job is very small, so there is only one section heading, . Reading the docs, I see that in order to set default values in a...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
13
by: Nader | last post by:
Hello, I have a dictionary and will get all keys which have the same values. d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)} I will something as : d.keys(where their...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.