Dictionary is one of the built-in data types in Python. Python’s dictionary is example of mapping type. A mapping object ‘maps’ value of one object with another.
Ex:
capitals = {"Telangana":"Hyderabad", "Karnataka":"Bengaluru"}
numbers = {10:"Ten", 20:"Twenty"}
d1 = {"Fruit":["Mango","Banana"], "Flower":["Rose", "Lotus"]}
d2 = {('India, USA'):'Countries', ('New Delhi', 'New York'):'Capitals'}
d3 = {["Mango","Banana"]:"Fruit", "Flower":["Rose", "Lotus"]} --> Error
get()
method in Python’s dict class returns the value mapped to the given key. dict.get("key")
gives value of the key.Updating dictionaries
There are few ways.
d1.update(d2)
.d1.update([(k1, v1), (k2, v2)])
d1.update(k1=v1, k2=v2)
Using the Union Operator ( | ). Ex: d3 = d1 | d2 Or d1 |= d2 |
d3 = {**d1, **d2}
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49}
print ("marks dictionary before update: \n", marks)
marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89}
newmarks = {**marks, **marks1}
print ("marks dictionary after update: \n", newmarks)
Result:
marks dictionary before update:
{'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
{'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Remove dictionary items
pop()
method of dict class causes an element with the specified key to be removed from the dictionary.popitem()
method in dict() class doesn’t take any argument. It pops out the last inserted key-value pair, and returns the same as a tuple. Ex: val = dict.popitem()clear()
method in dict class removes all the elements from the dictionary object and returns an empty object.Nested dictionaires
Ex:
marklist = {
"Mahesh" : {"Phy" : 60, "maths" : 70},
"Madhavi" : {"phy" : 75, "maths" : 68},
"Mitchell" : {"phy" : 67, "maths" : 71}
}
mydict = {'Name':'Test','Surname':'Test1','Age':36,'Salary':6500.20}
print("My name is :",mydict['Name'])
print("My Salary :",mydict['Salary'])
mydict['wife'] = "Testy"
print(mydict)
mydict['Name'] = "Test Tost"
print(mydict)
print(mydict.keys())
print(mydict.values())
print(mydict.items())
price_lookup= {'apple':2.99,'banana':1.99,'orange':1.49}
print("Price of Orange : ",price_lookup['orange'])
d = {'k1':123,'k2':[0,'a',2],'k3':{'Orange':1.49,'apple':1.99}}
print(d['k1'])
print("Dict list : ",d['k2'])
print("Dict list value : ",d['k2'][2])
print("Dict list value : ",d['k2'][1].upper())
print("Dict dict : ",d['k3'])
print("Dict dict value :",d['k3']['apple'])
Result:
My name is : Test
My Salary : 6500.2
{'Name': 'Test', 'Surname': 'Test1', 'Age': 36, 'Salary': 6500.2, 'wife': 'Testy'}
{'Name': 'Test Tost', 'Surname': 'Test1', 'Age': 36, 'Salary': 6500.2, 'wife': 'Testy'}
dict_keys(['Name', 'Surname', 'Age', 'Salary', 'wife'])
dict_values(['Test Tost', 'Test1', 36, 6500.2, 'Testy'])
dict_items([('Name', 'Test Tost'), ('Surname', 'Test1'), ('Age', 36), ('Salary', 6500.2), ('wife', 'Testy')])
Price of Orange : 1.49
123
Dict list : [0, 'a', 2]
Dict list value : 2
Dict list value : A
Dict dict : {'Orange': 1.49, 'apple': 1.99}
Dict dict value : 1.99
Main Page | Next Chapter: Python - Tuples |