May 7, 2025
dictionary_name = {}.capitals = {
"USA": "Washington DC",
"India": "New Delhi",
"China": "Beijing",
"Russia": "Moscow"
}
dir(): Lists all attributes and methods of a dictionary.
dir(capitals).help(): Provides in-depth description of attributes and methods.get() method to retrieve values:
capitals.get("USA") returns "Washington DC".None.if capitals.get("Japan"):
print("Capital exists")
else:
print("Capital doesn't exist")
update() to add or change key-value pairs.
capitals.update({"Germany": "Berlin"}).capitals.update({"USA": "Detroit"}).pop(): Removes key-value pair by key.
capitals.pop("China").popitem(): Removes the last inserted key-value pair.clear() method removes all items.keys() method: keys = capitals.keys().for key in capitals.keys():
print(key)
values() method: values = capitals.values().for value in capitals.values():
print(value)
items() method: items = capitals.items().for key, value in capitals.items():
print(f"{key}: {value}")
get, update, pop, popitem, clear, keys, values, items.