Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python map() dictionary values

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?

like image 262
Taj Morton Avatar asked May 21 '12 20:05

Taj Morton


People also ask

How do you map a dictionary value?

Write a Python program to map the values of a given list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value. Use map() to apply fn to each value of the list. Use zip() to pair original values to the values produced by fn.

Can I use map on dictionary Python?

We can use the Python built-in function map() to apply a function to each item in an iterable (like a list or dictionary) and return a new iterator for retrieving the results. map() returns a map object (an iterator), which we can use in other parts of our program.

How do you map a value in Python?

How Map in Python is Used as an Iterator. You can use the Python map() function with a String. While using the map() with a String, the latter will act like an array. You can then use the map() function as an iterator to iterate through all the string characters.

How do you apply a function to all values of a dictionary in Python?

You can use the toolz. valmap() function to apply a function to the dictionary's values. Similarly, to apply function to keys of a dictionary, use toolz. keymap() function and to apply function to items of a dictionary, use toolz.


1 Answers

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)
like image 116
senderle Avatar answered Nov 11 '22 19:11

senderle