The Python Oracle

Python Dictionary: "in" vs "get"

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping

--

Chapters
00:00 Python Dictionary: &Quot;In&Quot; Vs &Quot;Get&Quot;
00:54 Accepted Answer Score 19
01:11 Thank you

--

Full question
https://stackoverflow.com/questions/4650...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #dictionary #hash

#avk47



ACCEPTED ANSWER

Score 19


get() returns the value for the given key, if the key exists in the dict.

in returns a boolean value depending on if the key is present in the dict.

Use get() if you need the value. Use in if you only need to test if the key exists.