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 trialDan Lutsch
3,979 PointsPython: 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.
- about_me.update({"gender" : "male"})
- about_me["gender"] = "male"
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYes. 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
3,979 PointsDan Lutsch
3,979 PointsI see! It's good to know index style is faster than update! Thank you Chris!