What is Python Dictionary?
Dictionaries in python is one such concept, which is unique and gives an edge to python over other programming languages. It is a collection like a list or set but has unique features which will be discussed in this blog. So let us start by taking a look at pointers that will be discussed here:
- What Is Dictionaries In Python?
- Why Use A Dictionary In Python?
- List vs Dictionary
- How To Implement A Dictionary?
- Operations In Dictionaries :
- Accessing An Element
- Replacing An Element
- Removing An Element
- Other Operations
- Use Case – Nested Dictionary
It is a collection data type just like a list or a set, but there are certain features that make python dictionary unique. A dictionary in python is not ordered and is changeable as well. We can make changes in a dictionary unlike sets or strings which are immutable in nature. The dictionaries contains key-value pairs like a map that we have in other programming languages. A dictionary has indexes. Since the value of the keys we declare in a dictionary are always unique, we can use them as indexes to access the elements in a dictionary.
Why Use A Dictionary In Python
First of all, it is not like any other object or data type in python programming language. A dictionary has key value pairs resembling a map. It is often used for unordered data with distinct key values.
It is similar to a real life dictionary. We have distinct keys which we can use to fetch the values specified in them. In case of dictionary, even though there are no duplicate members, we can mention duplicate members in the value elements. Now that we know, why we use a dictionary, lets try to understand how it is different from lists in python.
Lists vs Dictionary
|Access elements use index values||Access elements use keys as index values|
|Collection of elements||Collection of key value pairs|
|Allows duplicate members||No duplicate members|
|Preferred for ordered data||Preferred for data with unique key values|
How To Implement A Dictionary
To declare a dictionary in python, we use the curly brackets. The keys and values are separated with a colon and the pairs are separated with a comma.
Operations In A Python Dictionary
1.) Accessing an element
2.) Replacing an element
3.) Removing an element
4.) Other Operations
Following are the operations we have for dictionary in python:
clear( ) – removes all the elements from the dictionary.
copy( ) – returns a copy of the dictionary.
values( ) – returns all the values in a dictionary.
update( ) – it updates the values of the dictionary with the specified key value pairs.
fromkeys( ) – returns a dictionary with the specified keys and values.
items( ) – returns the list for a tuple of each key value pair in the dictionary.
keys( ) – returns a list containing all the keys in the dictionary.
pop( ) – removes the element with the specified key.
popitem( ) – removes the last inserted key values pair from the dictionary.
setdefault( ) – returns the value of the specified key, if not present insert the key with the specified value.
Dict( ) constructor
Dictionary constructor is used to declare a dictionary in python.
Use Case – Nested Dictionary
Nested dictionary is nothing but a dictionary which incorporates another dictionary. Let us implement a dictionary where we have the statistical data for all the Indian batsmen. We will implement a dictionary with the batsmen names and incorporate other dictionary with the statistics inside the same dictionary to make it a nested dictionary. We will also use the pandas library to get the stats into a dataframe for better understanding.
This will print the data frame of the stats of the batsmen from the dictionary. Similarly, you can make this dictionary for bowlers, all rounders and wicket keepers to practice the implementation of nested dictionary.
In this blog, we have understood the concept of dictionary in python. We have learnt to implement a dictionary and various operations that we can perform on a dictionary. We also covered dictionary constructor and nested dictionary.
Till then, Stay Safe, Stay Happy & Keep Coding…
IF YOU WANT TO KNOW ABOUt DATA SCIENCE click here.
CONNECT WITH THE AUTHOR ON LINKEDIN.
VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT LEARNING.