How to change all dictionary keys in a for loop using d.items ()?

I would like to help with understanding why this code does not work as expected.

If you want to change the dictionary key, but save its value, it can use:

d[new_key] = d.pop[old_key]

I want to change all the keys (and keep the values ​​in place), but the code below skips certain lines - ("col2") remains untouched. Is it because the dictionaries are disordered and I keep changing the values ​​in it?

How can I change keys and save values ​​without creating a new dictionary?

import time
import pprint

name_dict = {"col1": 973, "col2": "1452 29th Street",
             "col3": "Here is a value", "col4" : "Here is another value",
             "col5" : "NULL", "col6": "Scottsdale",
             "col7": "N/A", "col8" : "41.5946922",
             "col9": "Building", "col10" : "Commercial"}


for k, v in name_dict.items():
    print("This is the key: '%s' and this is the value '%s'\n" % (k, v) )
    new_key = input("Please enter a new key: ")
    name_dict[new_key] = name_dict.pop(k)
    time.sleep(4)

pprint.pprint(name_dict)
+3
source share
3 answers

, . dict :

name_dict = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}

for k, v in name_dict.items():
    name_dict.pop(k)

RuntimeError:

. . , , , . , , {1: 1, 3: 3, 5: 5}, ( Python 3.6, 3.6 ):

hash    key    value
   -      -        - 
   1      1        1
   -      -        - 
   3      3        3
   -      -        - 
   5      5        5
   -      -        - 
   -      -        - 
   -      -        - 

, . , ( 1: 1). , 2 1, :

hash    key    value
   -      -        - 
   -      -        - 
   2      2        1
   3      3        3
   -      -        - 
   5      5        5
   -      -        - 
   -      -        - 
   -      -        - 

, "" , 2: 1. Oups...

, - ( ), .

3.6 , - .

, :

name_dict = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}

for k, v in name_dict.items():
    # print(k, k+6, name_dict.__sizeof__())
    name_dict[k+6] = name_dict.pop(k)
    # print(name_dict)

:

key   value
  1       1
  2       2
  3       3
  4       4
  5       5
  6       1

1, 7. 3.6, , 1 :

key   value
  -       -
  2       2
  3       3
  4       4
  5       5
  6       1
  7       2

, 4 10.

key   value
  -       -
  -       -
  -       -
  -       -
  5       5
  6       1
  7       2
  8       3
  9       4
 10       5

5 11 . - : :

key   value
  6       6
  7       1
  8       2
  9       3
 10       4
 11       5

, 5 , 6. 6 11: 5 . Oups...

, : ( )!

" " ( , " ", - , )

translate = {}
for k, v in name_dict.items():
    print("This is the key: '%s' and this is the value '%s'\n" % (k, v) )
    new_key = input("Please enter a new key: ")
    translate[k] = new_key
    time.sleep(4)

for old, new in translate.items():
    name_dict[new] = name_dict.pop(old)
+3

python3 dict.items() - dict. , dict, dict.items(). ()

for k, v in list(name_dict.items()):
    ...
    name_dict[new_key] = name_dict.pop(k)

" ", .

,

for k in list(name_dict):
    v = name_dict.pop(k)
    ...
    name_dict[new_key] = v

EDIT: , .

kv = list(name_dict.items())
name_dict.clear()
for k, v in kv :
    ...
    name_dict[new_key] = v

, dict, - .

+1

, , fromkeys. . , : , , .

Old_Keys = { old_key_1, old_key_2, ..., old_key_n }

, , , .

old_key_1  ->  new_key_1 not in Old_Keys  # Okay!
old_key_2  ->  new_key_2 == old_key_4     # Boom!... Error!...

, :

CODE

D = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

for key in D.fromkeys(D) :
    new_key = raw_input("Old Key: %s, New Key: " % key)
    D[new_key] = D.pop(key)

print D

CONSOLE

Old Key: key1, New Key: abc

Old Key: key2, New Key: def

Old Key: key3, New Key: ghi

{"abc": 'val1', "def": 'val2', "ghi": 'val3'}
+1

Source: https://habr.com/ru/post/1684693/


All Articles