Python Dictionary
● Dictionary holds key:value pair which are separated by comma and enclosed
by curly bracket({ }).
● Key: it belongs to tuple, number and string data type
● Value: it belongs to any data type like as number, string, Boolean, list,
tuple and dictionary
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)
output: {1: 'Geeks', 2: 'For', 3: 'Geeks'}
Creating an empty Dictionary
Dict = {}
Adding elements to a Dictionary
One value at a time can be added to a Dictionary by defining value along with the key
Syntax : Dict_name[Key] = ‘Value’.
# Creating an empty Dictionary
Dict = { }
print("Empty Dictionary: ")
print(Dict)
# Adding elements one at a time
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
# Adding set of values to a single Key
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
output: {0: 'Geeks', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}
Updating elements to a Dictionary
Syntax. Dict_name[Key] = ‘Value’.
e.g. dict= {0: 'Geeks', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}
dict[2]=”roshan”
print(dict)
output: {0: 'Geeks', 2: 'roshan', 3: 1, 'Value_set': (2, 3, 4)}
Accessing elements of a Dictionary
In order to access the items of a dictionary refer to its key name. Key can be used
inside square brackets.
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using key
print("Accessing a element using key:")
print(Dict['name'])
output: For
get() that will also help in accessing the element from a dictionary.
Syntax : dict_name.get(key)
Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
print("Accessing a element using get:")
print(Dict.get(3))
output: Geeks
Accessing an element of a nested dictionary
In order to access the value of any key in the nested dictionary, use indexing []
syntax.
Creating a Dictionary
Dict = {'Dict1': {1: 'Geeks'}, 'Dict2': {'Name': 'For'}}
# Accessing element using key
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
output: {1: 'Geeks'}
Geeks
For
Deleting Elements using del Keyword
The items of the dictionary can be deleted by using the del keyword as given below.
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
print(Dict)
del(Dict[1])
print(Dict)
Output {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
{'name': 'For', 3: 'Geeks'}
dictionary methods
1. clear() - Remove all the elements from the dictionary
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}
print(dict1.clear)
output: {}
2. copy()- Returns a copy of the dictionary
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}
dict2 = dict1.copy()
print(dict2)
output: {1: 'Python', 2: 'Java', 3: 'Ruby', 4: 'Scala'}
3. items() - Returns a list containing a tuple for each key value pair
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}
print(dict1.items())
output: ([(1, 'Python'), (2, 'Java'), (3, 'Ruby'), (4, 'Scala')])
4. keys() - Returns a list containing dictionary’s keys
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}
print(dict1.keys())
output: ([1, 2, 3, 4])
5. values()-Returns a list of all the values of dictionary
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}
print(dict1.values())
output: (['Python', 'Java',’ Ruby’,'Scala'])
6. update(dict2): Updates The update() method adds element(s) to the dictionary
if the key is not in the dictionary. If the key is in the dictionary, it updates the key
with the new value.
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update(d1)
print(d)
d1 = {3: "three"}
# adds element with key 3
d.update(d1)
print(d)
{1: 'one', 2: 'two'}
{1: 'one', 2: 'two', 3: 'three'}
The pop() method removes the specified item from the dictionary.
car = {“brand": "Ford", “model": "Mustang", "year": 1964}
car.pop("model")
print(car)
output: {“brand": "Ford", "year": 1964}