name_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}

for row in rows:
    # Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
    row = dict((name_map[name], val) for name, val in row.iteritems())
    ...

Or in Python2.7+ with Dict Comprehensions:

for row in rows:
    row = {name_map[name]: val for name, val in row.items()}
Answer from elo80ka on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-mapping-key-values-to-dictionary
Python - Mapping Key Values to Dictionary - GeeksforGeeks
July 12, 2025 - This dictionary comprehension iterates over indices of the k list and assigns each key from k to its corresponding value from v using k[i]: v[i]. It ensures that mapping is created efficiently without requiring external functions making it a ...
Discussions

Mapping over values in a python dictionary - Stack Overflow
@chiborg: that's because rather than look up all key-value pairs in one go, you are now using number-of-keys times my_dictionary.__getitem__ calls. 2014-10-15T15:21:15.25Z+00:00 ... Note that since PEP3113 (implemented in python 3.x) tuple parameters are not supported anymore: lambda (k,v): ... More on stackoverflow.com
🌐 stackoverflow.com
Python dictionary: mapping multiple keys to a unique value?
Absolutely - no requirement for the value to be unique. >>> dictionary_example = {0: 0, 1:0, 2: 0, 3:1, 4:1} >>> for i in range(5): ... print(dictionary_example[i]) ... 0 0 0 1 1 More on reddit.com
🌐 r/learnpython
6
3
March 9, 2019
How can I map keys to multiple values in a dictionary? (multidict) here's my code:
from collections import defaultdict first_dict = {'a': 1, 'b': 2, 'c': 3} second_dict = defaultdict(list) for key in first_dict.keys(): for value in first_dict.values() second_dict[key].append(value) print(second_dict) or better for key in first_dict: second_dict[key].append(list(first_dict.values())) print(second_dict) More on reddit.com
🌐 r/learnpython
9
2
April 25, 2020
Python map() dictionary values - Stack Overflow
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: Copys = 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... More on stackoverflow.com
🌐 stackoverflow.com
🌐
30 Seconds of Code
30secondsofcode.org › home › python › map dictionary values
Python - Map dictionary values - 30 seconds of code
August 3, 2024 - Luckily, there's a quick and easy way to do this in Python. Using dict.items(), you can iterate over the dictionary. Then, you can assign the values produced by the function to each key of a new dictionary.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module2_EssentialsOfPython › DataStructures_II_Dictionaries.html
Data Structures (Part II): Dictionaries — Python Like You Mean It
A nice syntax for creating a dictionary is to specify key-value pairs inside “curly braces”: {key1:value1, key2:value2, ...}. As an example, let’s construct a dictionary that maps types of foods to “fruit” or “vegetable”. We’ll start by mapping “apple” to “fruit”, and ...
🌐
PyPI
pypi.org › project › map-dictionary-keys
map-dictionary-keys · PyPI
In the above example, mapped_dictionary will be mapped according to the function some_mapping_function as follows: ... This function will work for all levels of nested dictionaries. In the following example, both 'sub_dictionary' and 'key_name' will be converted as per the mapping function ...
      » pip install map-dictionary-keys
    
Published   May 06, 2020
Version   1.0.0
🌐
Mark Needham
markhneedham.com › blog › 2020 › 04 › 27 › python-select-keys-from-map-dictionary
Python: Select keys from map/dictionary | Mark Needham
April 27, 2020 - Or we can iterate over all the entries in the map and filter it that way: >>> {key:value for key,value in x.items() if key in ["a", "b"]} {'a': 1, 'b': 2} This approach is longer but more flexible. For example, we could find the keys and values for all entries with a value great than 2 with the following code: >>> {key:value for key,value in x.items() if value > 2} {'c': 3, 'd': 4}
Find elsewhere
🌐
Python
docs.python.org › 2.5 › lib › typesmapping.html
3.8 Mapping Types -- dict
December 23, 2008 - Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. A dictionary's keys are almost arbitrary values. Only values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys.
🌐
Open Book Project
openbookproject.net › thinkcs › python › english3e › dictionaries.html
20. Dictionaries — How to Think Like a Computer Scientist: Learning with Python 3
The keys method returns what Python 3 calls a view of its underlying keys. A view object has some similarities to the range object we saw earlier — it is a lazy promise, to deliver its elements when they’re needed by the rest of the program. We can iterate over the view, or turn the view ...
🌐
w3resource
w3resource.com › python-exercises › dictionary › python-data-type-dictionary-exercise-70.php
Python: Map dictionary - w3resource
# Convert the zipped pairs into a dictionary where the elements from 'itr' are keys, and the results of 'fn' are values. return dict(zip(itr, map(fn, itr))) # Call the 'test' function with an iterable [1, 2, 3, 4] and a lambda function that squares its input. # The lambda function calculates the square of each element in the iterable. result = test([1, 2, 3, 4], lambda x: x * x) # Print the resulting dictionary. print(result) ... Write a Python program to convert a list of numbers into a dictionary where each key is the number and its value is the square of the number.
🌐
CodeRivers
coderivers.org › blog › python-map-dictionary
Python Map Dictionary: A Comprehensive Guide - CodeRivers
March 26, 2025 - In this code, we first define a function double that doubles a number. Then we use map() to apply this function to the values of the dictionary. Finally, we create a new dictionary with the original keys and the mapped values.
🌐
Real Python
realpython.com › python-dicts
Dictionaries in Python – Real Python
April 8, 2026 - You’ll find them behind core ... and locals(): ... The globals() function returns a dictionary containing key-value pairs that map names to objects that live in your current global scope....
🌐
Python Morsels
pythonmorsels.com › using-dictionaries-in-python
Using dictionaries in Python - Python Morsels
August 5, 2025 - Python's dictionaries act as lookup tables which map keys to their values.
Top answer
1 of 2
19

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)
2 of 2
0

You want the set-union of all the values of counters? I.e.,

counters[1].union(counters[2]).union(...).union(counters[n])

? That's just functools.reduce:

import functools

s = functools.reduce(set.union, counters.values())


If counters.values() aren't already sets (e.g., if they're lists), then you should turn them into sets first. You can do it using a dict comprehension using iteritems, which is a little clunky:

>>> counters = {1:[1,2,3], 2:[4], 3:[5,6]}
>>> counters = {k:set(v) for (k,v) in counters.iteritems()}
>>> print counters
{1: set([1, 2, 3]), 2: set([4]), 3: set([5, 6])}

or of course you can do it inline, since you don't care about counters.keys():

>>> counters = {1:[1,2,3], 2:[4], 3:[5,6]}
>>> functools.reduce(set.union, [set(v) for v in counters.values()])
set([1, 2, 3, 4, 5, 6])
🌐
W3Schools
w3schools.com › python › python_dictionaries.asp
Python Dictionaries
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... Dictionaries are used to store data values in key:value pairs.
🌐
Medium
medium.com › pythoneers › understanding-how-maps-work-in-python-ce7102539bad
Understanding How Maps Work in Python | by Rajat Sharma | The Pythoneers | Medium
April 13, 2024 - In programming, a map, also known as a dictionary, hash map, or associative array, is a data structure that stores items in a collection where each item is associated with a key. This key-value pair enables fast and efficient retrieval of values ...
🌐
Mohan Pudasaini
pudasainimohan.com.np › post › python_dictionary
Python Dictionaries: Key-Value Pair Mapping | Mohan Pudasaini
February 15, 2023 - Unlike sequences that store objects by their relative position, mappings use a unique key to store objects. This key-based approach makes Python dictionaries incredibly versatile ...
🌐
Real Python
realpython.com › python-mappings
Python Mappings: A Comprehensive Guide – Real Python
July 23, 2024 - When you access a key that’s missing from the dictionary, the key is added, and the default value is assigned to it. You can also create the same points_default object using the callable int as the first argument since calling int() with no arguments returns 0. All mappings are also collections, which means they’re iterable containers with a defined length. You can explore these characteristics with another mapping in Python’s standard library, collections.Counter: