Python map() dictionary values

dictionary, lambda, python

Solution

In Python 3, `map` returns an iterator, not a list. You still have to iterate over it, either by calling `list` on it explicitly, or by putting it in a `for` loop. But you shouldn't use `map` this way anyway. `map` is really for collecting return values into an iterable or sequence. Since neither `print` nor `set.update` returns a value, using `map` in this case isn't idiomatic.

Your goal is to put all the keys in all the counters in `counters` into a single set. One way to do that is to use a nested generator expression:

s = set(key for counter in counters.values() for key in counter)

There's also the lovely dict comprehension syntax, which is available in Python 2.7 and higher (thanks Lattyware!) and can generate sets as well as dictionaries:

s = {key for counter in counters.values() for key in counter}

These are both roughly equivalent to the following:

s = set()
for counter in counters.values():
    for key in counter:
        s.add(key)

Problem

I'm trying to use `map()` on the `dict_values` object returned by the `values()` function on a dictionary. However, I can't seem to be able to `map()` over a `dict_values`: ``` map(print, h.values()) Out[31]: <builtins.map at 0x1ce1290> ``` I'm sure there's an easy way to do this. What I'm actually trying to do is create a `set()` of all the `Counter` keys in a dictionary of `Counters`, doing something like this: ``` # counters is a dict with Counters as values whole_set = set() map(lambda x: whole_set.update(set(x)), counters.values()) ``` Is there a better way to do this in Python?

Original source