0

get method for dictionaries

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } x = car.get("model") y = car["model"] print(x) print(y) Hey guys, just wanted a better understanding of the "get" method used in the dictionary. what is the difference between x and y??

21st Nov 2021, 1:42 AM
Simon Varghese
Simon Varghese - avatar
1 ответ
+ 3
.get() is the safe way off accessing keys. Usually used with a second argument: what to return if the key isn't in the dictionary. So it'd be: x = car.get("model", None) try: z = car.get("mirrors", None) print(z)
21st Nov 2021, 1:46 AM
Slick
Slick - avatar