Lecture 5 Dictionary
Lecture 5 Dictionary
1 Python Dictionary
Difficulty Level : Easy Last Updated : 31 Oct, 2021 Dictionary in Python is an unordered collection
of data values, used to store data values like a map, which, unlike other Data Types that hold only a
single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary
to make it more optimized.
2 Creating a Dictionary
In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces,
separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corre-
sponding pair element being its Key:value. Values in a dictionary can be of any data type and can
be duplicated, whereas keys can’t be repeated and must be immutable.
[11]: # Creating a Dictionary
# with Integer Keys
List = [1, 3, 'as', 56]
print(Dict.values())
print(Dict.keys())
1
Dictionary with the use of Mixed Keys:
{'Name': ['Ali', 'ahmed', 'shoib'], 'roll no': [1, 23, 12]}
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'BSAI', 2: 'For', 3:'AI'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'BSAI'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Empty Dictionary:
{}
2
print(Dict)
Empty Dictionary:
{}
# Creating a Dictionary
Dict = {1: 'BSAI', 'name': 'For', 3: 'AI'}
3
print(Dict['name'])
{1: 'AI'}
AI
For
[15]: myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
4
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
[22]: child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
myfamily
5
# Deleting a Key value
del Dict[6]
print("\nDeleting a specific key: ")
print(Dict)
Initial Dictionary:
{5: 'Welcome', 6: 'To', 7: 'BSAI', 'A': {1: 'BSAI', 2: 'For', 3: 'AI'}, 'B': {1:
'Amazing', 2: 'Life'}}
# Deleting a key
# using pop() method
pop_ele = Dict1.pop(1)
print('\nDictionary after deletion: ' + str(Dict1))
print('Value associated to poped key is: ' + str(pop_ele))
6
# Deleting an arbitrary key
# using popitem() function
pop_ele = Dict.popitem()
print("\nDictionary after deletion: " + str(Dict))
print("The arbitrary pair returned is: " + str(pop_ele))
10 Built in Function
[8]: str(Dict)
[9]: len(Dict)
[9]: 2
[10]: type(Dict)
[10]: dict
[11]: Dict.clear()
[12]: Dict.copy()
[12]: {}
7
[3]: Dict.fromkeys()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-0daed0f5138e> in <module>
----> 1 Dict.fromkeys()
thisdict = dict.fromkeys(x, y)
print(thisdict)
thisdict = dict.fromkeys(x)
8
print(thisdict)
[20]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x)
[2]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"persons": {'Ali':2, "ahmed": 3, 'bilal':4}
x = car.items()
car["year"] = 2018
print(x)
[ ]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"persons": {'Ali':2, "ahmed": 3, 'bilal':4}
9
print(i)
print(j)
10
10.4 Question: Check if a value exists in a dictionary.
[5]: # Dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
value_to_check = 2
10.5 Question: Find the key with the maximum value in a dictionary.
[6]: # Dictionary
my_dict = {'a': 10, 'b': 20, 'c': 30}
11
my_dict.pop(key_to_remove, None)
12