19

I have dictionary like

d = {'user_id':1, 'user':'user1', 'group_id':3, 'group_name':'ordinary users'}

and "mapping" dictionary like:

m = {'user_id':'uid', 'group_id':'gid', 'group_name':'group'}

All i want to do is "replace" keys in first dictionary with values from the second one. The expected output is:

d = {'uid':1, 'user':'user1', 'gid':3, 'group':'ordinary users'}

I know that keys are immutable and i know how to do it with 'if/else' statement.

But maybe there is way to do it in one line expression?

1
  • uid is the value in the second, not the key. What is the output you like to see? Jan 15, 2011 at 9:57

4 Answers 4

26

Let's take the excellent code from @karlknechtel and see what it does:

>>> d = dict((m.get(k, k), v) for (k, v) in d.items())
{'gid': 3, 'group': 'ordinary users', 'uid': 1, 'user': 'user1'}

But how does it work?

To build a dictionary, you can use the dict() function. It expects a list of tuples. In 3.x and >2.7, you can also use dictionary comprehension (see answer by @nightcracker).

Let's dissect the argument of dict. At first, we need a list of all items in m. Every item is a tuple in the format (key, value).

>>> d.items()
[('group_id', 3), ('user_id', 1), ('user', 'user1'), ('group_name', 'ordinary users')]

Given a key value k, we could get the right key value from m by doing m[k].

>>> k = 'user_id'
>>> m[k]
'uid'

Unfortunately, not all keys in d also exist in m.

>>> k = 'user'
>>> m[k]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'user'

To work around that, you can use d.get(x, y), which returns d[x] if the key x exists, or the default value y if it doesn't. Now, if a key k from d doesn't exist in m, we just keep it, so the default is k.

>>> m.get(k, k).
'user'

Now we are ready to build a list of tuples to supply to dict(). To build a list in one line, we can use list comprehension.

To build a list of squares, you would write this:

>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]

In our case, it looks like this:

>>> [(m.get(k, k), v) for (k, v) in d.items()]
[('gid', 3), ('uid', 1), ('user', 'user1'), ('group', 'ordinary users')]

That's a mouthful, let's look at that again.

Give me a list [...], which consists of tuples:

[(.., ..) ...]

I want one tuple for every item x in d:

[(.., ..) for x in d.items()]

We know that every item is a tuple with two components, so we can expand it to two variables k and v.

[(.., ..) for (k, v) in d.items()]

Every tuple should have the right key from m as first component, or k if k doesn't exist in m, and the value from d.

[(m.get(k, k), v) for (k, v) in d.items()]

We can pass it as argument to dict().

>>> dict([(m.get(k, k), v) for (k, v) in d.items()])
{'gid': 3, 'group': 'ordinary users', 'uid': 1, 'user': 'user1'}

Looks good! But wait, you might say, @karlknechtel didn't use square brackets.

Right, he didn't use a list comprehension, but a generator expression. Simply speaking, the difference is that a list comprehension builds the whole list in memory, while a generator expression calculates on item at a time. If a list on serves as an intermediate result, it's usually a good idea to use a generator expression. In this example, it doesn't really make a difference, but it's a good habit to get used to.

The equivalent generator expressions looks like this:

>>> ((m.get(k, k), v) for (k, v) in d.items())
<generator object <genexpr> at 0x1004b61e0>

If you pass a generator expression as argument to a function, you can usually omit the outer parentheses. Finally, we get:

>>> dict((m.get(k, k), v) for (k, v) in d.items())
{'gid': 3, 'group': 'ordinary users', 'uid': 1, 'user': 'user1'}

There happens quite a lot in one line of code. Some say this is unreadable, but once you are used to it, stretching this code over several lines seems unreadable. Just don't overdo it. List comprehension and generator expressions are very powerful, but with great power comes great responsibility. +1 for a good question!

25

Sure:

d = dict((m.get(k, k), v) for (k, v) in d.items())
3
  • 2
    it should be for (k, v) in d.items not for (k, v) in d
    – mouad
    Jan 15, 2011 at 10:02
  • Fixed. (And re-fixed; items needs to be called as a method.) Jan 15, 2011 at 10:06
  • 2
    For Python 2.x, it would be more appropriate to use d.iteritems() instead of d.items() (but this only matters for really big dictionaries). Jan 15, 2011 at 13:55
9

In 3.x:

d = {m.get(key, key):value for key, value in d.items()}

It works by creating a new dictionary which contains every value from d and mapped to a new key. The key is retrieved like this: m[key] if m in key else key, but then with the default .get function (which supports default values if the key doesn't exist).

0
2

Why would you want to do it in one line?

result = {}
for k, v in d.iteritems():
    result[m.get(k, k)] = v
2
  • 2
    Because list/dict/any comprehensions are very pythonesque and blazing fast?
    – orlp
    Jan 15, 2011 at 10:41
  • "Pythonic", but yes, pretty much that. Jan 18, 2011 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.