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
#1
Hello

I have this dictionary:
{('i', 'i'): (0.4958635668040591+0.033985852749267645j),
 ('i', 'x'): (-3.713537562982453e-06-2.578284945296621e-07j),
 ('i', 'y'): (-2.078134606106431e-06-1.5092272785217425e-07j),
 ('i', 'z'): (0.0030247632238068057+0.00019935846367565656j),
 ('x', 'i'): (-0.000497513570319244-5.014558284085448e-05j),
 ('x', 'x'): (3.4616869917723378e-09+2.9221799051462875e-10j),
 ('x', 'y'): (1.9383437449053815e-09+1.5103701730941268e-10j),
 ('x', 'z'): (-0.006595795711769758-0.00046843232709893433j),
 ('y', 'i'): (-0.48228842229892616-0.03305438712887293j),
 ('y', 'x'): (3.6119889496561947e-06+2.5261831345886037e-07j),
 ('y', 'y'): (2.021496707024936e-06+1.449570267430537e-07j),
 ('y', 'z'): (-0.0029419875232505823-0.00019382466419900108j),
 ('z', 'i'): (-3.1576270223704965e-06-5.4375273822120716e-08j),
 ('z', 'x'): (5.0875697722712674e-12+1.9453039138714473e-12j),
 ('z', 'y'): (4.816868975153029e-13-1.8167517982790014e-12j),
 ('z', 'z'): (-0.0004005193083767186-2.7382736070473568e-05j),
 "('i', 'i')i": (0.4928883854032347+0.03378193763277204j),
 "('i', 'x')i": (-3.6912563376045584e-06-2.562815235624841e-07j)}
I want to update the key values and I want to add 'i' at the end of the key values like that:
{('i', 'i','i'): (0.4958635668040591+0.033985852749267645j),
 ('i', 'x', 'i'): (-3.713537562982453e-06-2.578284945296621e-07j),
 ('i', 'y','i'): (-2.078134606106431e-06-1.5092272785217425e-07j),
etc...
How to do that?
Reply
#2
The thing on the left is usually called a "key" and the thing on right is a "value".

There's no way to really "modify" a key. Instead you would create the new key and delete the old one.

But odder to me is that your keys are not all the same type. Most are tuples and some are strings. The way to "add i to it" depends on the type. If it was always a tuple, you could do something like:

import pprint
d = {('i', 'i'): (0.4958635668040591+0.033985852749267645j),
 ('i', 'x'): (-3.713537562982453e-06-2.578284945296621e-07j),
 ('i', 'y'): (-2.078134606106431e-06-1.5092272785217425e-07j),
 ('i', 'z'): (0.0030247632238068057+0.00019935846367565656j),
 }

d = {(*k, "i"): v for k,v in d.items()}

pprint.pprint(d)
Output:
{('i', 'i', 'i'): (0.4958635668040591+0.033985852749267645j), ('i', 'x', 'i'): (-3.713537562982453e-06-2.578284945296621e-07j), ('i', 'y', 'i'): (-2.078134606106431e-06-1.5092272785217425e-07j), ('i', 'z', 'i'): (0.0030247632238068057+0.00019935846367565656j)}
But if some of your keys aren't tuple, you'd have to do more work. Probably loop over the original dict, then create the necessary key/value in a new one based on whether the first key is a tuple or a string.
quest likes this post
Reply
#3
Amazing thanks fro the quick answer :)
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change value in a nested dictionary? nzcan 2 5,776 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