+ 1
Immutable objects
How is this possible if Immutable objects can't be changed? __________________________________________________ https://code.sololearn.com/cpuDeCGPZvA5/?ref=app
2 ответов
+ 7
dictionaries are mutable. their keys are immutable.
https://artofproblemsolving.com/wiki/index.php/Dictionary
https://docs.python.org/3/tutorial/datastructures.html#dictionaries
+ 7
🇮🇳Omkar🕉 , you are absolutely right about keys in a dictionary. However there is a "work around" that can be applied in one line of code.
The key (plus the value) that should be modified will be removed with pop(). in the same line a key with the desired value will be created. The new key is not in the same sequence as the old key has been, it will appear at the end of the dictionary.
squares = { 3: "sololearn", 4: 16,}
print(squares)
# in next line the key "3" will be modified to "11"
squares[11] = squares.pop(3)
print(squares)