In [4]: a="123"
print(type(a))
<class 'str'>
In [7]: a={"Name":"Aastha","Class":11,"Sub":"BST"}
print(a)
{'Name': 'Aastha', 'Class': 11, 'Sub': 'BST'}
In [8]: a={}
print(a)
{}
In [11]: a={"Name":"Aastha","Class":11,"Sub":"BST"}
print(a["Name"])
Aastha
In [12]: print(a["Class"])
11
In [15]: print(a["Sub"])
BST
In [18]: a={"Name":"Aastha Kapoor","Class":11,"Sub":"BST"}
print(a)
{'Name': 'Aastha Kapoor', 'Class': 11, 'Sub': 'BST'}
Traversing a dictionary
In [2]: d={"IP":19,"BST":26,"Hindi":6,"English":26}
for key in d: #here key will contain all the keys of a dictionary
print(key,":",d[key])
IP : 19
BST : 26
Hindi : 6
English : 26
In [17]: d={"IP":19,"BST":26,"Hindi":6,"English":26}
for i in d: #i=IP,BST,Hindi,English
print("Information:",i,d[i])
Information: IP 19
Information: BST 26
Information: Hindi 6
Information: English 26
In [1]: d1={1:"A",2:"B",3:"C",1:"D"} #keys must be unique
print(d1) #here value of key 1 is updated by latest value 'D'
{1: 'D', 2: 'B', 3: 'C'}
In [3]: d1={2:"B",3:"C",1:"D"}
print(d1)
d1[3]="Z" #dictionary is mutable ,values can be changed
print(d1)
{2: 'B', 3: 'C', 1: 'D'}
{2: 'B', 3: 'Z', 1: 'D'}
Accessing keys and values of a dictionary
In [4]: d1={2:"B",3:"C",1:"D"}
print(d1.keys())
print(d1.values())
dict_keys([2, 3, 1])
dict_values(['B', 'C', 'D'])
Adding key:value pair to empty dictionary
In [7]: emp={}
print(emp)
emp['Name']='Aastha' #adding key:value pair
print(emp)
{}
{'Name': 'Aastha'}
Creating dictionary from key and value
pairs
In [8]: Student=dict(name="Akash",age=10,city="pune")
print(Student)
{'name': 'Akash', 'age': 10, 'city': 'pune'}
Nested Dictionaries
In [1]: emp={"name":"Sophia","age":25,
"address":{"H.No":121,"Street":"Mira road","City":"Pune"}}
print(emp)
{'name': 'Sophia', 'age': 25, 'address': {'H.No': 121, 'Street': 'Mira ro
ad', 'City': 'Pune'}}
In [2]: emp["address"]
Out[2]: {'City': 'Pune', 'H.No': 121, 'Street': 'Mira road'}
In [3]: emp["address"]["City"]
Out[3]: 'Pune'
In [5]: emp["address"]["H.No"]
Out[5]: 121
In [6]: emp["city"]
-------------------------------------------------------------------------
--
KeyError Traceback (most recent call las
t)
<ipython-input-6-093fb2468967> in <module>()
----> 1 emp["city"]
KeyError: 'city'
Deleting elements in dictionary
In [1]: d={1:10,2:20,3:30,4:40}
print(d)
d.pop(2)
print(d)
{1: 10, 2: 20, 3: 30, 4: 40}
{1: 10, 3: 30, 4: 40}
In [3]: d={11:10,12:20,13:30,14:40}
print(d)
d.pop(12)
print(d)
{11: 10, 12: 20, 13: 30, 14: 40}
{11: 10, 13: 30, 14: 40}
In [6]: d={11:10,12:20,13:30,14:40}
print(d)
print(d.pop(2,"Not found"))
print(d)# d is not changed as key 2 is not present in d
{11: 10, 12: 20, 13: 30, 14: 40}
Not found
{11: 10, 12: 20, 13: 30, 14: 40}
Pretty printing a dictionary
In [7]: d={"a":"apple","b":"boy","c":"cat","d":"dog"}
print(d)
{'a': 'apple', 'b': 'boy', 'c': 'cat', 'd': 'dog'}
In [15]: import json
d={"a":"apple","b":"boy","c":"cat","d":"dog"}
print(json.dumps(d,indent=2))
{
"a": "apple",
"b": "boy",
"c": "cat",
"d": "dog"
}
items() method
In [2]: sweets={1:"Kaju Katli",2:"Laddoo",3:"Gulab Jamun",4:"Chocolate"}
for i in sweets.items():
print(i)
(1, 'Kaju Katli')
(2, 'Laddoo')
(3, 'Gulab Jamun')
(4, 'Chocolate')
In [4]: for i,j in sweets.items():
print(i,j)
1 Kaju Katli
2 Laddoo
3 Gulab Jamun
4 Chocolate
fromkeys() method
In [1]: a=('name','age','salary')
d=dict.fromkeys(a)
print(d)
{'name': None, 'age': None, 'salary': None}
In [3]: a=('name','age','salary')
v=100
d=dict.fromkeys(a,v)
print(d)
{'name': 100, 'age': 100, 'salary': 100}
Copy method
In [4]: d1={"name":"Astha","age":20,"Salary":10000}
d2=d1.copy()
print(d1)
print(d2)
{'name': 'Astha', 'age': 20, 'Salary': 10000}
{'name': 'Astha', 'age': 20, 'Salary': 10000}
In [6]: d2["age"]=15
print(d2)
print(d1)
{'name': 'Astha', 'age': 15, 'Salary': 10000}
{'name': 'Astha', 'age': 20, 'Salary': 10000}
In [9]: d={"Name":"Ramesh","Age":25,"Salary":70000}
k,v=d.popitem()
print(k,v)
print(d)
Salary 70000
{'Name': 'Ramesh', 'Age': 25}
In [10]: d={"Name":"Ramesh","Age":25,"Salary":70000}
a=d.popitem()
print(a)
print(d)
('Salary', 70000)
{'Name': 'Ramesh', 'Age': 25}
WAP to count frequency of elements in a
list
In [2]: L=[10,20,30,10,1,20,30,2,1,3,4,10]
d={}
for i in L: #i=10,20,30..
if i not in d:
d[i]=L.count(i) #d[10]=3
print(d)
{10: 3, 20: 2, 30: 2, 1: 2, 2: 1, 3: 1, 4: 1}
Program to create dictionary for storing employees
and their salaries and finding sum of salaries
In [5]: n=int(input("Enter how many employees you want to enter:"))
d={}
for i in range(0,n):
a=input("Enter name of employee:")
b=int(input("Enter salary of employee:"))
d[a]=b
print("final dictionary:",d)
sum1=0
for i in d:
sum1=sum1+d[i]
print("Sum of salaries:",sum1)
Enter how many employees you want to enter:3
Enter name of employee:Ayush
Enter salary of employee:1000
Enter name of employee:Bhavya
Enter salary of employee:2000
Enter name of employee:Chitra
Enter salary of employee:3000
final dictionary: {'Ayush': 1000, 'Bhavya': 2000, 'Chitra': 3000}
Sum of salaries: 6000
In [ ]: n=int(input("Enter how many states you want to enter:"))
d={}
for i in range(0,n):
a=input("Enter name of state:")
b=int(input("Enter capital of state:"))
d[a]=b
print("State capital pair are as follows:")
print(d)
In [ ]: