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 trial

Python

Anyway to rename a key directly instead of deleting and re-entering a new key?

Not sure if it will be covered later, but if I have a lot of information already contained in one key and don't wanna delete the key just to rename it.

1 Answer

Hi Harris,

If you are sure that the old key exists, you can do the following:

my_dict[new_key] = my_dict.pop(old_key)

Cheers

Alex

Wow, that's smart, didn't think of that! Thanks!