Python dictionary is an ordered collection (starting from Python 3.7) of items. It stores elements in key/value pairs. Here, keys are unique identifiers that are associated with each value.
capital_city = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}
print(capital_city)
capital_city = {"Nepal": "Kathmandu", "England": "London"}
print("Initial Dictionary: ",capital_city)
capital_city["Japan"] = "Tokyo"
print("Updated Dictionary: ",capital_city)
We can also use [] to change the value associated with a particular key. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
print("Initial Dictionary: ", student_id)
student_id[112] = "Stan"
print("Updated Dictionary: ", student_id)
In Python, we use the keys to access their corresponding values. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
print(student_id[111]) # prints Eric
print(student_id[113]) # prints Butters
We use the del statement to remove an element from the dictionary. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
print("Initial Dictionary: ", student_id)
del student_id[111]
print("Updated Dictionary ", student_id)
We can also delete the whole dictionary using the del statement,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
# delete student_id dictionary
del student_id
print(student_id)# Output: NameError: name 'student_id' is not defined
Methods that are available with a dictionary are tabulated below. Some of them have already been used in the above examples.
#all()
s = {0: 'False', 1: 'False'}
print(all(s))
s = {1: 'True', 2: 'True'}
print(all(s))
s = {1: 'True', False: 0}
print(all(s))
s = {}
print(all(s))
# 0 is False
# '0' is True
s = {'0': 'True'}
print(all(s))
#any()
# 0 is False
d = {0: 'False'}
print(any(d))
# 1 is True
d = {0: 'False', 1: 'True'}
print(any(d))
# 0 and False are false
d = {0: 'False', False: 0}
print(any(d))
# iterable is empty
d = {}
print(any(d))
# 0 is False
# '0' is True
d = {'0': 'False'}
print(any(d))
testDict = {1: 'one', 2: 'two'}
print(testDict, 'length is', len(testDict))
testDict = {}
print(testDict, 'length is', len(testDict))
#Len in Object
class Session:
def __init__(self, number = 0):
self.number = number
def __len__(self):
return self.number
# default length is 0
s1 = Session()
print(len(s1))
# giving custom length
s2 = Session(6)
print(len(s2))
#sort()
py_dict = {'e': 1, 'a': 2, 'u': 3, 'o': 4, 'i': 5}
print(sorted(py_dict, reverse=True))
We can test if a key is in a dictionary or not using the keyword in. Notice that the membership test is only for the keys and not for the values.
# Membership Test for Dictionary Keys
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
# Output: True
print(1 in squares) # prints True
print(2 not in squares) # prints True
# membership tests for key only not value
print(49 in squares) # prints false
We can iterate through each key in a dictionary using a for loop.
# Iterating through a Dictionary
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])
In Python, we use the keys to access their corresponding values. For example,