Given a dictionary { k1: v1, k2: v2 ... }
I want to get { k1: f(v1), k2: f(v2) ... }
provided I pass a function f
.
Is there any such built in function? Or do I have to do
dict([(k, f(v)) for (k, v) in my_dictionary.iteritems()])
Ideally I would just write
my_dictionary.map_values(f)
or
my_dictionary.mutate_values_with(f)
That is, it doesn't matter to me if the original dictionary is mutated or a copy is created.
dict((k, f(v)) for k, v in mydict.iteritems())
, i.e. without the square brackets, that would prevent the creation of an intermediate list via a generator. – Astronomer