Suppose that we have a Python dictionary,
x = {'a': 3, 'b': 2, 'c':7}
and, we want to add the values i.e. 3 + 2 + 7 = 12.
Here are the three ways to add the values.
Using list comprehension
>>> x = {'a': 3, 'b': 2, 'c':7}
>>> sum([i for i in x.values()])
12
Using for loop I
>>> x = {'a': 34, 'b': 2, 'c':7}
>>> total = 0
>>> for i in x.values():
total += i
Using for loop II
>>> x = {'a': 34, 'b': 2, 'c':7}
>>> total = 0
>>> for key in x.keys():
total += x[key]
Creates the exact copy of your hard disk and allows you to instantly restore the entire machine.
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010








Why is list comprehension needed?
Surely you can just do:
>>> x = {'a': 3, 'b': 2, 'c':7}
>>> sum(x.values())
12