You are on page 1of 7

Dictionary

 Python dictionary is an unordered collection of


items enclosed in curly bracket where each item is
a key-value pair.
 Each key is separated by :
 Keys are unique whereas values may repeat.
 The value of a dictionary can be of any data type.
but key must be of immutable data type.
 Dictionary is mutable. We can add item, delete item
and modify value.
Example:
D = {“Jan”:31, “Feb”:28, “Mar”:31}

Creating Empty Dictionary


1.
D = {}

2.
D = dict()

>>> D={}
>>> type(D)
<class 'dict'>
>>> T=dict()
>>> type(T)
<class 'dict'>
>>> print(D)
{}
>>> print(T)
{}

Creating dictionary with values


>>> D={"Jan":31,"Feb":28,"Mar":31}
>>> print(D)
{'Jan': 31, 'Feb': 28, 'Mar': 31}

>>> D={"Jan":31,"Feb":28,"Mar":31,"Feb":29}
>>> D
{'Jan': 31, 'Feb': 29, 'Mar': 31}
Accessing Dictionary items/elements

>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31}
>>> D["Feb"]
28

Adding/updating elements in dictionary


>>> D={}
>>> D
{}
>>> D["Jan"]=31
>>> D
{'Jan': 31}
>>> D["Feb"]=28
>>> D
{'Jan': 31, 'Feb': 28}
>>> D["Mar"]=31
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31}
>>> D["Apr"]=30
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> D["Jan"]=30
>>> D
{'Jan': 30, 'Feb': 28, 'Mar': 31, 'Apr': 30}

Traversing

D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}
for I in D:
print(I)

Jan
Feb
Mar
Apr
D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}
for I in D:
print(D[I])

31
28
31
30

D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}
for I in D:
print(I,":",D[I])

Jan : 31
Feb : 28
Mar : 31
Apr : 30
Membership Operators
in, not in

>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> "Feb" in D
True
>>> "May" in D
False
>>> "May" not in D
True
>>> "Apr" not in D
False

"WAP to create a dictionary to store N names and their


mobile numbers"

N=int(input("Enter size:"))
D={}
for I in range(N):
nm=input("Enter Name:")
phone=int(input("Enter mobile No:"))
D[nm]=phone

print(D)

Functions

1. len() : it is used to count the number of key-value


pairs.
>>> D
{'Jan': 31, 'Feb': 28}
>>> len(D)
2

2. clear() : it removes all the key value pairs.


>>> D
{'Jan': 31, 'Feb': 28}
>>> D.clear()
>>> D
{}

3. get() :it returns the value for the specified key. if


specified key is not present then it returns None by
default.
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> print(D["Feb"])
28
>>> print(D.get("Feb"))
28
>>> print(D.get("May"))
None
>>> print(D.get("May","Not Found"))
Not Found
>>> print(D.get("Apr","Not Found"))
30
4. keys() : it returns the list of all the keys.
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> print(D.keys())
dict_keys(['Jan', 'Feb', 'Mar', 'Apr'])
>>> K=D.keys()
>>> K
dict_keys(['Jan', 'Feb', 'Mar', 'Apr'])
>>> type(K)
<class 'dict_keys'>
>>> list(K)
['Jan', 'Feb', 'Mar', 'Apr']
>>> for I in K:
print(I)

D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}

for I in D.keys():
print(I)
5. Values() : it returns the list of all the keys.
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> D.values()
dict_values([31, 28, 31, 30])
>>> K=D.values()
>>> K
dict_values([31, 28, 31, 30])
>>> for I in K:
print(I)

31
28
31
30

D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}

for I in D.values():
print(I)

6. items() :it returns all the key-value pairs in a form


of list of tuples.
>>> D
{'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30}
>>> D.items()
dict_items([('Jan', 31), ('Feb', 28), ('Mar', 31),
('Apr', 30)])

D={"Jan":31,"Feb":28,"Mar":31,"Apr":30}

for I,J in D.items():


print(I,J)

Jan 31
Feb 28
Mar 31
Apr 30

You might also like