Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates. Dictionaries are written with curly brackets, and have keys and values: {'Key':'Value'}
Note:As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
D = {}
print(D)
D = {'Name':'Kevin','Age':30,'Marks':89.78}
print(D)
print(D['Name'])
print(D['Age'])
print(D['Marks'])
D = {'Name':'Kevin','Age':30,'Marks':89.78,'Age':56}
print(D)
print(D)
print(len(D))
print(type(D))
D = {
"brand": "Tesla",
"electric": True,
"year": 2021,
"colors": ["black", "white", "red"]
}
print(D)
print(D)
print(D['brand'])
There is also a method called get() that will give you the same result:
print(D.get('brand'))
print(D.keys())
print(D.values())
The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
x = car.keys()
print(x) #before the change
car["color"] = "black"
print(x) #after the change
Make a change in the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
x = car.values()
print(x) #before the change
car["color"] = "black"
print(x) #after the change
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
print(car.items())
Make a change in the original dictionary, and see that the items list gets updated as well:
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
x = car.items()
print(x) #before the change
car["color"] = "black"
print(x) #after the change
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
if "brand" in car:
print("Yes, 'brand' is one of the keys in the car dictionary")
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
"color": 'black'
}
print(car)
car['color'] = 'white'
print(car)
car.update({"year": 2022})
print(car)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021,
}
print(car)
car['color'] = 'black'
print(car)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021
}
print(car)
car.update({"color": "black"})
print(car)
print(car)
car.pop('electric')
print(car)
print(car)
del car['year']
print(car)
The del keyword can also delete the dictionary completely:
del car
print(car)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021
}
car.clear()
print(car)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021
}
for i in car:
print(i)
for i in car:
print(car[i])
for i in car.values():
print(i)
for i in car.keys():
print(i)
for i in car.items():
print(i)
for i,j in car.items():
print(i,j)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021
}
car2 = car
print(car)
print(car2)
car['year']=2022
print(car)
print(car2)
car = {
"brand": "Tesla",
"electric": True,
"year": 2021
}
car2 = car.copy()
print(car)
print(car2)
car['year']=2022
print(car)
print(car2)
myfamily = {
"child1" : {
"name" : "Kevin",
"year" : 2009
},
"child2" : {
"name" : "Ronaldo",
"year" : 2011
},
"child3" : {
"name" : "Keisha",
"year" : 2013
}
}
print(myfamily)
print(myfamily['child1'])
print(myfamily['child1']['name'])
print(myfamily['child1']['year'])
d={'Name':['Kevin','Ranaldo','Keisha'],'Age':[31,33,34]}
print(d)
print(d['Name'])
print(d['Age'])
print(d.keys())
print(d.values())
print(d.items())
for i,j in d.items():
print(i,j)
for i,j in d.items():
print("Key:",i)
for val in j:
print(val)
print()