Articles → Python → Dictionary In Python
Dictionary In Python
What Is A Dictionary?
Syntax
Variable_name = {“key name1”: value1, “key name 2: value 2………….}
Variable_name[“key name”]
Example
personInformation = {"Name": "Karan", "Age":37}
print(personInformation)
print(personInformation["Name"])
Delete From The Dictionary
personInformation = {"Name": "Karan", "Age":37}
print(personInformation)
# Delete Key from dictionary
del personInformation ["Age"]
print(personInformation)
Clear All Items From The Dictionary
personInformation = {"Name": "Karan", "Age":37}
print(personInformation)
# Clear all items from dictionary
personInformation.clear()
print(personInformation)
Loop Through All Key-Value Pair In The Dictionary
personInformation = {"Name": "Karan", "Age":37}
for k,v in personInformation.items():
print("Key:",k,", Value:",v)
The Uniqueness Of Elements In The Dictionary
dict = {"key1":1, "key1":2}
print(dict)
Nested Dictionary
nested_dictionary = {"name": "Karan", "address": {"city":"Gurgaon", "country": "India"}}
print("Name:", nested_dictionary["name"])
print("city:", nested_dictionary["address"]["city"])
print("country:", nested_dictionary["address"]["country"])
Delete The Whole Dictionary
personInformation = {"Name": "Karan", "Age":37}
print(personInformation)
del(personInformation)
print(personInformation)