Python Forum
Change the key value in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change the key value in dictionary
#4
Keys need to be immutable for a simple reason. The underlying data structure is called a hash table and key/value pairs are placed into buckets which allows for fast lookup of the key. Which bucket an item ends up in is determined by computing a number that represents the key (its hash value). If you were then to modify a key, there's a chance that lookup would fail because the hash of the new key could mean it should be found in a different bucket. So, the dictionary would need to recompute the hash on a modification, which I assume is not done for reasons of efficiency and complexity.
Reply


Messages In This Thread
Change the key value in dictionary - by quest - Mar-25-2022, 01:42 AM
RE: Change the key value in dictionary - by quest - Mar-25-2022, 04:47 AM
RE: Change the key value in dictionary - by ndc85430 - Mar-25-2022, 07:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change value in a nested dictionary? nzcan 2 5,804 Sep-23-2019, 04:09 PM
Last Post: nzcan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020