Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
There are 2 ways.
If there is a need to simply copy the values of one dictionary from another, simply assigning one name to another will not work.
dict1 = {1: "один", 2: "два"}
dict2 = dict1 # Так делать НЕЛЬЗЯ!
dict1 = {1: "один", 2: "два"}
dict2 = dict1.copy() # Так делать ПРАВИЛЬНО!
dict1 = {1: "один", 2: "два"}
dict2 = {}
for key in dict1:
dict2[key] = dict1[key]
dict1 = {1: "один", 2: "два"}
dict2 = {}
for key, item in dict1.items():
dict2[key] = item
dict1 = {1: "один", 2: "два"}
dict2 = {}
for keys in dict1:
dict2[keys] = dict1[keys] * 2
Dictionaries have an update method that adds new values to the dictionary and overwrites old ones:
dict1 = {1: 'one', 2: 'two'}
dict2 = {3: 'three'}
dict2.update(dict1)
dict2
>> {3: 'three', 1: 'one', 2: 'two'}
a = {1: 111, 2: 222, 3: 333}
b = {1: 444, 2: 555, 4: 666, 5: 777}
b.update(a)
b
>> {1: 111, 2: 222, 4: 666, 5: 777, 3: 333}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question