1

In Python, I'm trying to rename the itens from dicio2, but the code is stopping before the last one:

here, I have this dict with the number and number's name:

dicio1 = {"1":"one",
          "2":"two",
          "3":"three",
          "4":"four",
          "5":"five",
          "6":"six"}

and dicio2 have the number with a value:

dicio2 = {"1":"QWE",
          "2":"QWE",
          "3":"QWE",
          "4":"QWE",
          "5":"QWE",
          "6":"QWE"}

This is what I need in final run:

dicio2 = {'one': 'QWE',
          'two': 'QWE',
          'three': 'QWE',
          'four': 'QWE',
          'five': 'QWE',
          'six': 'QWE'}

I used this code:

for v in dicio2:
    if v in dicio1:
        dicio2[dicio1[v]] = dicio2.pop(v)

but the result is:

{'6': 'QWE',
 'one': 'QWE',
 'two': 'QWE',
 'three': 'QWE',
 'four': 'QWE',
 'five': 'QWE'}

after that, I put a print(v):

for v in dicio2:
    print(v)
    if v in dicio1:
        dicio2[dicio1[v]] = dicio2.pop(v)

And the result was that:

1
2
3
4
5
five
{'6': 'QWE', 'one': 'QWE', 'two': 'QWE', 'three': 'QWE', 'four': 'QWE', 'five': 'QWE'}

someone can help me why this happens? how can I do that with all the itens without crash?

The perfect mode that I needed is to rename the key without change your position, but i saw in other topic that is not possible because the key cannot be rennamed and the solution is only use the POP command to delete and create another.

  • 1
    use a different variable in your loops, not `v` for both – SuperStew Jan 06 '20 at 18:51
  • 1
    Looks like `dict1` and `dict2` are pretty much trying to be lists. You may want to reconsider that design of sequential integer keys for these structures. – ggorlen Jan 06 '20 at 18:54
  • Does this answer your question? [How to delete items from a dictionary while iterating over it?](https://stackoverflow.com/questions/5384914/how-to-delete-items-from-a-dictionary-while-iterating-over-it) – norok2 Jan 06 '20 at 18:56

4 Answers4

2

Maybe you can just do this:

output = {dicio1[k]: v for k, v in dicio2.items()}
print(output)
>>> {'one': 'QWE', 'two': 'QWE', 'three': 'QWE', 'four': 'QWE', 'five': 'QWE', 'six': 'QWE'}
marcos
  • 4,473
  • 1
  • 10
  • 24
  • can you explain me this syntax? I'm a beginner in Python – Henrique Oliveira Costa Jan 06 '20 at 19:37
  • It will iterate over `dicio2` elements, `k` is your key and `v` the value. It will use the current key from `dicio2` and access the value from `dicio1` and use it a key for the output. It will just use the values from `dicio2`. Consider studying dictionary comprehension https://www.python.org/dev/peps/pep-0274/. Good luck @HenriqueOliveiraCosta – marcos Jan 06 '20 at 19:46
1
dicio1 = {"1":"one",
          "2":"two",
          "3":"three",
          "4":"four",
          "5":"five",
          "6":"six"}

dicio2 = {"1":"QWE",
          "2":"QWE",
          "3":"QWE",
          "4":"QWE",
          "5":"QWE",
          "6":"QWE"}

dicio2 = dict( (dicio1[k], dicio2[k]) for k,_ in dicio2.items() if k in dicio1)
aminrd
  • 4,300
  • 4
  • 23
  • 45
1
dicio1 = {"1":"one",
          "2":"two",
          "3":"three",
          "4":"four",
          "5":"five",
          "6":"six"}

dicio2 = {"1":"QWE",
          "2":"QWE",
          "3":"QWE",
          "4":"QWE",
          "5":"QWE",
          "6":"QWE"}

temp = dict()
for k, v in dicio1.items():
    if k in dicio2:
        temp[v] = dicio2[k]
dicio2 = temp
print(dicio2)

Output: {'one': 'QWE', 'two': 'QWE', 'three': 'QWE', 'four': 'QWE', 'five': 'QWE', 'six': 'QWE'}

codrelphi
  • 1,075
  • 1
  • 7
  • 13
1

You are not allowed to modify the keys or change the size of dicio2 during the iteration. Use dicio1 for the iteration and then modify dicio2

for key in dicio1:
    if key in dicio2:
        newKey = dicio1.get(key)
        value = dicio2.pop(key)
        dicio2[newKey] = value
Funwie
  • 91
  • 4