In this post, you’ll learn the dictionary in Python. And how to create, adding or removing elements from the dictionary in python. We will take examples with python in-built functions on the dictionary.
Python Dictionary
The dictionary list created in Python is very simple. You will learn everything about Python dictionary here. In order to understand the better, we also have many examples with Python dictionary and with in-built Python methods.
What is the dictionary in Python?
In Python programming, a dictionary is an unordered item collection list and it’s mutable. The dictionary is defined by having values between curly brackets { } and kept data into key and value pairs.
The meaning of mutable is that the dictionary list can change the item.
If you know the keys in the dictionary. So it becomes easy to get their value
Python create dictionary
We have explained about lists, tuple, and sets in previous python tutorials. These are different from Python dictionary. Now we will know how to create a dictionary in Python.
Let’s take an example. In which we will create a python dictionary:
# python dictionary my_dict = {'name': 'john', 'age': '28'} print(my_dict)
Output
{'name': 'john', 'age': '28'}
Access item in dictionary python
So far we have learned how to make a dictionary in Python. But now we will know how to access the dictionary in Python.
To access an item from the dictionary in Python, we use the item key. We can get the item’s value from the dictionary by passing the item key inside square brackets.
Now, we will create a new dictionary and access it’s item:
# python dictionary my_dict = {'name': 'john', 'age': '28'} print(my_dict['name'])
Output
john
You have another method of Python called get (). You can also do this to get the value of Python dictionary.
Even in this method, only the name of the key has to be passed. And it will give you a dictionary messaging value.
# python dictionary my_dict = {'name': 'john', 'age': '28'} print(my_dict.get['name'])
Output
john
Change dictionary value python
Here you will see how you can change the value of the already created dictionary in python.
Here we are updating the values for existing key-value pairs. We are using this key to update a value in the dictionary in python.
To understand better you can see the example given below:
my_dict = { "name": "Sam", "age": "25", "year": 1995 } my_dict["year"] = 2018 print(my_dict)
Output
{'name': 'Sam', 'age': '25', 'year': 2018}
Python in-built methods
Now we will tell you about some Python in-built methods that you can use with Python dictionary.
You can do add, update, remove, etc. to Python dictionary with these methods.
add item to dictionary python
Let us now show you how to add items in the Python dictionary.
To add the item to the Python dictionary using a new index key and assigning a value to it
You can see its example below:
my_dict = { "name": "Sam", "age": "25", "year": 1995 } my_dict["next_year"] = 2018 print(my_dict)
Output
{'name': 'Sam', 'age': '25', 'year': 1995, 'next_yer': 2018}
Find length of set in python
If you have any dictionary and want to get their length. So you have to use the len() function of Python.
Python’s len() function also finds the length of sets, lists, tuples, dictionary etc.
Python: Find the length of a dictionary example:
my_dict = { "name": "Sam", "age": "25", "year": 1995 } print(len(my_dict))
Output
3
Remove item to dictionary python
Python has many methods to remove items from the dictionary such as pop() method, del keyword, etc. By using these Python built-in methods, items from the Python dictionary can be removed.
Remove item from dictionary python using pop()
my_dict = { "name": "Sam", "age": "25", "year": 1995 } my_dict.pop("age") print(my_dict)
Output
{'name': 'Sam', 'year': 1995}
Python delete operation on dictionary
You can also delete the antire dictionary of Python with the key value pair. For this, you have to use clear () method of Python.
The clear () method of Python deletes the entire dictionary.
To understand the clear () method of Python, you can do the example below. And when using the clear () method, you can also see the output you got:
my_dict = { "name": "Sam", "age": "25", "year": 1995 } my_dict.clear() print(my_dict)
Output
{ }
Python Dictionary Methods
We have given a list of built-in python methods below. There are many methods on this list. Which you can use with the Python dictionary.
The built-in Python methods are given in this list. Some of these methods have examples with the Python dictionary above.
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key value pair |
keys() | Returns a list containing the dictionary’s keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |