-1

I would like to know how can I access list values in a dictionary to sum them:

{'key1': [3, 1], 'key2': [0, 4], 'key3': [57492, 204652], 'key4': [1,3]}

I would like to sum the first element of each list sum(3,0,57492,1).

Is it possible to do so without any loop ?

Thank you in advance

Unic0
  • 341
  • 1
  • 3
  • 19
  • 3
    Have you tried anything at all? Have you looked at how to iterate over the values of a dictionary? – juanpa.arrivillaga Apr 19 '18 at 04:29
  • Yes, but every time I had to use a loop and I thought there was a way to access all the values in one go without iterating over all the dictionary. – Unic0 Apr 19 '18 at 04:39
  • 3
    One way or another, you *have* to iterate over the dictionary, whether that involves a list-comprehension, a for-loop, a while-loop, or some loop buried in the internals of `map`. – juanpa.arrivillaga Apr 19 '18 at 04:39

1 Answers1

3

You have to iterate over the entire dictionary which means you have to use loop.

It can simply be done with a list comprehension like:

sum([values[0] for key, values in dictionary.items()])

If the number of items in the dictionary are large in number then instead of dictionary.items() you can use a generator function

For Python 2.x:

for key, value in d.iteritems():

For Python 3.x:

for key, value in d.items():

You can read more on how to do list comprehension here: Link

Iterating over a dictionary: Link

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
  • First thank you very much, secondly would it be better if I use an iterator since I have a huge amount of values ? I read about it but I am fairly new in programming. – Unic0 Apr 19 '18 at 04:37
  • @Unic0 what is "huge"? If you want to use a memory-efficient verion, just use a generator-expression: `sum(v[0] for v in d.values())`. If you are on Python 2, use `d.itervalues()` instead. – juanpa.arrivillaga Apr 19 '18 at 04:38
  • Cool, yes you can use an iterator, but it will work the same way. List comprehension is also an iterator just written in one line in a simple way. – Vikash Singh Apr 19 '18 at 04:39
  • Also as @juanpa.arrivillaga pointed out you can use an iterator or a generator function if there are too many values in the list. – Vikash Singh Apr 19 '18 at 04:40
  • @VikashSingh a list-comprehension is not really an iterator. – juanpa.arrivillaga Apr 19 '18 at 04:40
  • Wow thank you ! Huge since I'll iterate over pixels values for images 640x640 and thousands of them ! – Unic0 Apr 19 '18 at 04:40
  • @Unic0 True, List comprehension is not an iterator, it's an iterative way to build a list. – Vikash Singh Apr 19 '18 at 04:45
  • 1
    @VikashSingh Thank you very much for the advice, I tried many things but I thought I would be clearer to just put the question rather that all the tries. But noted ! Thank you again – Unic0 Apr 19 '18 at 04:46
  • No issues, I remember I used to get down-voted a lot in the early days. Just feels nice to be able to help someone. So thought I will give you a pointer. For a safer side you can always share what you have tried so far. – Vikash Singh Apr 19 '18 at 04:48
  • People want to see that you have put effort yourself before posting the question. Hope it makes sense. See you around :) – Vikash Singh Apr 19 '18 at 04:50