Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMert Kahraman
12,118 PointsHow do we change the existing keys without changing their values?
We saw how to change the values for certain keys, but is there a way to directly change the key of a value as well? For example, can we change the key value pair "LGA" : "La Guardia" to "ABC" : "La Guardia" directly? Or do we need to delete the LGA one and add the ABC one?
2 Answers
Michael Hulet
47,913 PointsA dictionary maintains a relationship between a key and a value, but not between a value and a key. In other words, while there may be a valueForKey:
method, there's no keyForValue:
method, because keys can't be referenced by their value. Thus, the only way that's possible is the second method you described
Cindy Lea
Courses Plus Student 6,497 PointsCheck out this article. I think they give examples of what youre asking:
http://stackoverflow.com/questions/4406501/change-the-key-value-in-python-dictionary
Michael Hulet
47,913 PointsFYI: Your answer is in Python, but the question is in Swift