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 Python Collections (2016, retired 2019) Dictionaries Key Management

Dan Lutsch
Dan Lutsch
3,979 Points

Python: Update key and value in dictionary - What's the difference btwn dict.update() and dict["key"] = "value"?

I'm not quite sure the difference between these two methods. Are they same or different?

ex. about_me = {"job" : "student" , "name" : "Dan" , "gender" : "female"}

If I want to update "gender" I think I can use both methods.

  1. about_me.update({"gender" : "male"})
  2. about_me["gender"] = "male"

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Yes. Both methods are equally valid. One argument is for readability (which has votes on both sides!). In terms of performance, using indexing is about 3x faster than the update method [source StackOverflow].

Dan Lutsch
Dan Lutsch
3,979 Points

I see! It's good to know index style is faster than update! Thank you Chris!