Python Lesson – 5.3 Functions on dictionaries

Python Lessons - 5.3 Funtions on dictionaries m

In and Not In – Search for a key in a dictionary

Dictionaries already have a fairly complex structure and therefore various functions are needed to manage them. However, there are the clauses in and not in that let you know if a key is present in a dictionary or not, returning True in the first case, False in the second.

price = {"key" : 12, "door": 24, "lock": 18}
print( "key" in price)
print("handle" in price)

if you run it, you get:


The get method

If you have a dictionary, and you want to get the value by knowing the key, you can use the get() method. If the key is not present in the dictionary, it will return the None object. Furthermore, get () is a method that can also accept a second argument. In this case the second argument will be the value returned in case the key was not found in the dictionary.

price = {"key" : 12, "door": 24, "lock": 18}

if you run it, you get:


⇐ Go to Python Lesson 5.2 – Dictionaries

Go to Python Lesson 5.4 – Tuples ⇒

Leave a Reply