+ 1

Immutable objects

How is this possible if Immutable objects can't be changed? __________________________________________________ https://code.sololearn.com/cpuDeCGPZvA5/?ref=app

11th Oct 2020, 9:26 AM
Bane
Bane - avatar
2 Respuestas
+ 7
dictionaries are mutable. their keys are immutable. https://artofproblemsolving.com/wiki/index.php/Dictionary https://docs.python.org/3/tutorial/datastructures.html#dictionaries
11th Oct 2020, 9:29 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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)
11th Oct 2020, 11:08 AM
Lothar
Lothar - avatar