473,511 Members | 15,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum values stored in a dictionary as a list of values.

GTXY20
29 New Member
Hi All,

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
I need to sum the values for each key and make the dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried:


Expand|Select|Wrap|Line Numbers
  1. for item in f.values():
  2.                 for items in item:
  3.                     sum(items)
I am pretty sure I am way off here any advice where to start.
Oct 5 '07 #1
2 14789
bvdet
2,851 Recognized Expert Moderator Specialist
Hi All,

I have the following dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
I need to sum the values for each key and make the dictionary:

Expand|Select|Wrap|Line Numbers
  1. {'1': [20], '3': [20], '2': [20], '4': [14]}
I have tried:


Expand|Select|Wrap|Line Numbers
  1. for item in f.values():
  2.                 for items in item:
  3.                     sum(items)
I am pretty sure I am way off here any advice where to start.
This is one way:
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {'1': [10, 5, 4, 1], '3': [10, 5, 4, 1], '2': [10, 5, 4, 1], '4': [10, 4]}
  2. >>> dd2 = dict(zip(dd.keys(), [[sum(item)] for item in dd.values()]))
  3. >>> dd2
  4. {'1': [20], '3': [20], '2': [20], '4': [14]}
  5. >>> 
another one:
Expand|Select|Wrap|Line Numbers
  1. >>> dd3 = {}
  2. >>> for key in dd:
  3. ...     dd3[key] = [sum(dd[key])]
  4. ...     
  5. >>> dd3
  6. {'1': [20], '3': [20], '2': [20], '4': [14]}
  7. >>> 
You can also update the original:
Expand|Select|Wrap|Line Numbers
  1. >>> for key in dd:
  2. ...     dd[key] = [sum(dd[key])]
  3. ...     
  4. >>> dd
  5. {'1': [20], '3': [20], '2': [20], '4': [14]}
  6. >>> 
Oct 5 '07 #2
GTXY20
29 New Member
Perfect!

Thanks so much.
Oct 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2076
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
43871
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...
11
6054
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
14
3437
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
34141
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...
5
1950
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that...
13
12742
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...
10
1965
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
0
3068
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
7148
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
7430
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...
1
7089
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
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.