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??
1 Resposta
+ 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)