3

At the moment I have two dictionaries

dictionary1 = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
dictionary2 = {'value1': 'AAA', 'value3': 'CCC'}

I want to replace the values of dictionary1 with the values from dictionary2 which with the example above would produce the following output

{'key1': 'AAA', 'key2': 'value2', 'key3': 'CCC'}

From other examples I have read I think that I should begin my code as follows:

for key, value in dictionary1.iteritems():
    if value ==

Now I am unsure as to the best method to cycle through the other dictionary to find if the value from dictionary1 matches a key from dictionary2.

algorhythm
  • 3,304
  • 6
  • 36
  • 56

5 Answers5

8

You can do this with dictionary comprehension, like this

>>> dictionary1 = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
>>> dictionary2 = {'value1': 'AAA', 'value3': 'CCC'}
>>> {k: dictionary2.get(v, v) for k, v in dictionary1.items()}
{'key3': 'CCC', 'key2': 'value2', 'key1': 'AAA'}

Here, dict2.get(v, v) will try to get the value corresponding to the value v from dict2. If it is not found, the default value (second parameter), v itself will be returned.

Note 1: What you have in question, are called dictionaries, not lists.

Note 2: If you are using Python 2.7, then you might want to use dict1.iteritems or dict1.viewitems instead of dict1.items. The reason is explained, elaborately, with examples, in this answer.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    This is not working for me. I don't know why. It should, but it isn't. – TheTank Dec 19 '17 at 16:10
  • 1
    This should be `{k: dictionary2.get(k, v) for k, v in dictionary1.items()}`. Because the `get` method accepts the key as the first argument – Philipp May 03 '21 at 11:28
7

A cross lookup in the second list with a fallback to the default value if it doesn't exist is the way to go.

>>> {key : list2.get(value, value) for key, value in list1.items()}
{'key3': 'CCC', 'key2': 'value2', 'key1': 'AAA'}

get(key[, default]) allows you to lookup for the key in the dictionary and if the value is not present, the default value is returned. The default value should be the same value you would pursue to search in the second dictionary.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
2

You basically want to do a second for loop over your second dictionary until you find a matching value.

for key, value in list1.iteritems():
    for key2 in list2:
         if value == key2:
             list1[key] = list2[key2]
             break
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
  • 1
    runtime can be proportional to size(list1)*size(list2) , so it's not the good way to do that. – B. M. Apr 27 '15 at 17:36
2

For an in place change :

for k,v in dictionary1.items():
    if v in dictionary2 : dictionary1[k]=dictionary2[v]

it's faster than creating a new one.

B. M.
  • 18,243
  • 2
  • 35
  • 54
1

Adding this solution pretending to present dict comprehension solution with a more complex condition:

I have faced a similar situation but the goal was replacing matched keys in different dicts where dict has multiple values for each key-value pair. After a long try-error time with conditionals dict comprehensions, here my solution:

>>> result = {k: [dic2.get(v, v) for v in v] for k, v in dic1.iteritems()}

Figure:

>>> dic1 = {'key1': ['value1', 'value12', 'value13'], 'key2': ['value2', 'value21'] 'key3': ['value3']}
>>> dic2 = {'value1': 'path_AA', 'value12': 'path_BB',  'value2': 'path_DD', 'value21': 'path_EE', 'value3': 'path_FF'}
>>>
>>> result = {'key1': ['path_AA', 'path_BB'], 'key2': ['path_DD', 'path_EE'], 'key3': 'path_FF'}
AbreuFreire
  • 100
  • 2
  • 8