This is a bit more confusing, but you asked for a "pythonic-way";)
newD = {k:round(v) for k, v in d.items()}
However, this dictionary understanding will only work on 2.7+. If you are using an older version of Python, try to do it in a more confusing way:
newD = dict(zip(d.keys(), [round(v) for v in d.values()]))
Let me unpack a little:
- We start by reassigning the new dictionary (
d ) back to the new dictionary upon request (although you can easily assign it with the same name) - External
dict() provides the final result - a dictionary object zip() returns a list of tuples, where the i-th tuple contains the i-th element from each sequence of arguments- The first sequence of arguments provided by
zip() is the dictionary keys ( d.keys() ) - The second sequence of arguments given by
zip() are rounded values ββafter understanding the list. - Understanding the list rounds each value in dictionary values ββand returns a list of rounded values
source share