-1

I have the following 2 dictionaries

a = {'a': 1, 'b': 2, 'c': 3}
b = {1: 11, 2: 22}

And I'd like to modify a into

a = {'a': 11, 'b': 22, 'c': 3}

How do I achieve this result?

tainangao
  • 121
  • 1
  • 2
  • 9
  • 1
    What's your attempt so far? – Daniel Hao Sep 06 '21 at 12:45
  • Python dictionaries are unordered, and so you can't assume that "a" and "1" will be the first entry in each dictionary. Therefore this is not possible like this, you should use a list if you want the order to be meaningful – ithomas Sep 06 '21 at 12:46
  • "Python dictionaries are unordered ...." it's not 100% true any more. Please see this https://stackoverflow.com/questions/54849202/ – Daniel Hao Sep 06 '21 at 12:54

4 Answers4

2

You might use following dict-comprehension

a = {'a': 1, 'b': 2, 'c': 3}
b = {1: 11, 2: 22}
a = {k:b.get(v,v) for k,v in a.items()}
print(a)

output

{'a': 11, 'b': 22, 'c': 3}

Note usage of .get(v,v) so if there is not key in b original value is retained.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • You're assuming that "a" and "1" are the first entries, but a python dictionary is unordered so the code will not always give the same answer – ithomas Sep 06 '21 at 12:50
  • 1
    @ithomas - What does order matter when iterating over every key regardless of their position? – Sayse Sep 06 '21 at 12:51
  • @ithomas this depend on version, *dictionary is unordered* if you use ancient enough version of python, in 3.7 and newer as [Built-in Types docs](https://docs.python.org/3/library/stdtypes.html) put it *Dictionary order is guaranteed to be insertion order.* – Daweo Sep 06 '21 at 12:55
  • Yes, if you're using Python 3.5 or below this solution will not work correctly – ithomas Sep 06 '21 at 13:03
  • @ithomas then please provide such input data which would case "solution will not work correctly" in python 3.5 or below – Daweo Sep 06 '21 at 13:05
  • @ithomas It will work in exactly the same way. Order is irrelevant when updating values, since no new keys are added. – ekhumoro Sep 06 '21 at 13:07
  • @Sayse: I understood the question differently, that is why: I thought that the key of b refers to the dictionary entry, i.e. 1:11 = update the first entry with the value 11; 2:22 = update the second entry with the value 22. After seeing your answers, I now understand that the key of b refers to any value in a, in which case the answer above is always valid. Otherwise, if the question is in the form I originally understood, you would have to sort the dictionary beforehand: `keys = sorted(list(a)); a = {key:b[i+1] if i + 1 in b.keys() else a[key] for i, key in enumerate(keys)}` – ithomas Sep 06 '21 at 13:29
1

try this:

a = {'a': 1, 'b': 2, 'c': 3}
b = {1: 11, 2: 22}
for k,v in a.items():
        a[k] = b.get(v, v)
print(a)

Output:

{'a': 11, 'b': 22, 'c': 3}
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
1

you can try dict comprehension

{k1: b.get(v1, v1) for k1,v1 in a.items()}
{'a': 11, 'b': 22, 'c': 3}
Epsi95
  • 8,832
  • 1
  • 16
  • 34
1

Try this:

a = {k: b.get(v, v) for k, v in a.items()}
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59