Python dictionary
What is a Dictionary, give some real-life examples?
Example 1: A telephone directory is a good example, where the key is the name, and the value is the phone number.
Example 2: Suppose someone wants to search for a student in a university. He came to you and asked for the same.
Then you might ready with a list of questions like:- Which course, which semester, etc.
But against this list of questions, you can ask a single question, what is the student roll number?
If you get the roll number you will found the student detail and share. So in this example, the student roll number is the key and student details are the value.
Why do we need a Dictionary?
Keeps the key/value mapping format and internally uses hashing for it. Therefore, we can get a value from the dictionary by using its key very quickly.
What is the complexity of Python Dictionary?
Best case = O(1)
Worst case = O(n)
What are the different ways to create a Dictionary in Python?
There are 6 ways to create a Dictionary object in Python. some are as follows:
A. Create an Empty Dictionary:
Create an empty dictionary object without any data. it’s very simple and easy to define. There are 2 ways to create an Empty Dictionary object in Python.
1. By using empty brackets :
Code Example: myDictionary ={}
2. By using dict(), constructor:
Code Example: myDictionary=dict()
B. Create Dictionary with Literals:
Create a dictionary object with some data.
Code example: myDictionary={123:’Ankur’, 124:’Tishi’}
C. Creating Dictionary by passing parameters in dict() constructor:
Create a dictionary object by using dict() constructor with some parameters.
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
D. Creating Dictionary by a list of tuples.
we can create a dictionary object in python from a list of tuples.
Code example:
listOfTuples = [(123,’Ankur’),(124,’Tishi’)]
myDicitionary=dict(listOfTuples)
E. Creating Dictionary by list of keys and some default value.
Creating a Python Dictionary object by passing a list of keys and provide the default value of our choice. This the flexibility of Python Dictionary.
Code Example:
listOfRollNumber=[123,124]
myDicitionary=dict.fromkeys(listOfRollNumber, ‘Name’)
Note: it will internally iterate over the list, for each element it will create key-value pair with the default value provided and store then in the Dictionary object.
F. Creating a Python Dictionary object by two lists of the same number of elements.
This is a very good feature of Python Dictionary to create a dictionary object by using 2 different lists having the same number of elements.
Code Example:
listOfRollNumber=[123,124]
listOfName=[“Ankur”,”Tishi”]
myDictionary=dict(zip(listOfRollNumber,listOfName))
Important points to remember about the dictionary key
- Keys are always unique in a dictionary object.
- Keys must be immutable datatype (once key-value pair is added into the dictionary object then it can’t change the key itself but we can change the value associated with it.
Important points to remember about the dictionary value
A dictionary object can contain the value of any datatype.
code Example:
MyDictionary = dic(123=’Ankur’, 124=[‘Tishi’,’MTech’])
Access Dictionary item
Now we know how to create a dictionary in Python. let’s understand how to access an item in the dictionary object.
So there are 6 ways to access dictionary item. They are as follows:
A. by using IN operator:
suppose my dictionary looks like
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
Now access this object and print its all key along the value associated with it.
for item in myDictionary:
print(item,” = ”, myDictionary[item])
Output is
123 = Ankur
124 = Tishi
B. By using list comprehension.
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
print([item, myDictionary[item] for item in myDictionary])
C. By using dict.items() method.
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
for k,v in myDictionary.items():
print(k,” = ”, v)
D. By using enumerate().
for item in enumerate(myDictionary.items()):
print(item)
E. By using get() method.
There are 2 ways to use this approach.
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
- print(myDictionary.get(123))
- for item in myDictionary:
print(myDictionary.get(item))
Note: if the key doesn’t exist in the dictionary, then return None in this approach.
F. by using square brackets.
There are 2 ways to use this approach.
myDictionary = dict(123=’Ankur’, 124=’Tishi’)
- print(myDictionary[123])
- for item in myDictionary:
print(myDictionary[item])