The get() Method for Dictionaries in Python


Access dictionary items with a default value if the key doesn’t exist.

Source Code

my_dict = {"name": "Alice", "age": 30}
print(my_dict.get("name", "Unknown"))
print(my_dict.get("city", "Location not specified"))
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments