Dictionary in Python

Sakshi Dhameja  11 min read
21 Jul 2023


 Are you looking to become adept in the python language? Do you want to fast-track your mastery of the language by diving into its core components, like dictionaries? Then you may want to get into a Python course. As a programmer, understanding how to use and manipulate dictionaries is essential for a successful coding journey. In this blog post, we will explain what exactly a dictionary is in python programming as well as some tips and tricks on working with it effectively. Keep reading and soon you'll feel comfortable.

What is a dictionary in Python? 

A dictionary in python is a powerful tool for storing and organizing data. It allows complex data to be stored in an easily accessible manner, quickly allowing programmers to find the information they need. This makes dictionary objects a regular part of programming in python, due to their ease of use and effectiveness. Unlike other datatypes such as lists and tuples, dictionary values are unordered and can include any type of data, such as strings, integers, or even functions. Other features of dictionary datatypes include the ability to combine dictionaries together, update dictionary values no matter their size, and loop through dictionary objects for quick manipulation. Overall, dictionary datatypes are an essential part of programming with python language; using them gives maximum flexibility to store both simple and complex items.

Access Value in Dictionary in python

Accessing a value in a dictionary in Python is an incredibly simple process that is extremely useful when working with larger datasets. A dictionary is an unordered collection of data, and access to any value that it holds requires only the key associated with it. This makes retrieval fast and efficient since the program knows exactly which key to access without needing to search through the entire dataset. Additionally, this access can be done very easily by simply using brackets around the key, meaning accessing the value in the dictionary python has never been quicker or easier!


dict = {'Name': 'Urmi', 'Age': 8, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']
dict['Name']: Urmi
dict['Age']: 8

Update Dictionary in python

Updating a dictionary in python is easy and convenient. With the update() method, users can modify an existing dictionary by adding items to it or by changing the key-value pairs within it. To update a dictionary, the user needs to simply provide values for existing keys or add new key-value pairs. This update process will save a lot of time and effort when working with larger dictionaries that are constantly changing. In addition, this update() method allows users to efficiently update multiple dictionaries at once by merging all additional items into one. It ultimately simplifies complex operations within python and gives users the power to get things done quickly and easily.
dict = {'Name': 'Urmi', 'Age': 8, 'Class': 'First'}
dict['Age'] = 9; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']


dict['Age']: 9
dict['School']: DPS School

Delete Dictionary Elements in python

To delete an element from dictionary python from a dictionary in python is quite simple. It is done using the del keyword, which deletes the key-value pair from the dictionary. To delete an element from the dictionary, simply write "del dic[key]", where dic is the name of this particular dictionary and the key is the crucial value want to delete. Remember that keys in a python dictionary must be unique and immutable to index items correctly for searching and retrieval. Deleting specific items can help keep data organized and up to date.


dict = {'Name': 'Urmi', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']


Traceback (most recent call last):
   File "test.py", line 8, in <module>
      print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable

Properties of dictionary keys in python

Python dictionaries are powerful tools that allow developers to reference data stored within them quickly and easily. Dictionary keys in python provide a way to access stored values whenever they are needed. this has made the python dictionary key value essential for developers looking to create efficient software, due to their ability to make dictionary references nearly instant. Knowing how to leverage dictionary keys in Python can be the difference between writing great code that's fast and efficient or code that can slow down the program or cause crashes.
There are two major properties of a dictionary in the python language, which are
  • In the dictionary of the python language, more than one entry per key is not allowed, which means no duplicate key is allowed in python programs. When duplicate keys are encountered during an assignment, the last assignment wins.
dict = {'Name': 'Urmi', 'Age': 7, 'Name': 'Soma'}
print "dict['Name']: ", dict['Name']
dict['Name']: Soma
  • In the dictionary of the python language, the keys must be immutable, this means the user as well as the developer can use strings, numbers, or tuples as their dictionary keys but something like “key” is not allowed.
dict = {['Name']: 'Urmi', 'Age': 7}
print "dict['Name']: ", dict['Name']


Traceback (most recent call last):
   File "test.py", line 3, in <module>
      dict = {['Name']: 'Z', 'Age': 7};
TypeError: unhashable type: 'list'

Built-in Dictionary Functions

Sr.No. Function Description
1 cmp(dict1, dict2) Compares elements of both dict.
2 len(dict) Gives the total length of the dictionary which would be equal to the number of items in the dictionary
3 str(dict) Produces a printable string representation for that dictionary
4type(variable) Returns the type of the passed variable. If there is a passed variable is a dictionary, then it would return a dictionary type.

Built-in Dictionary methods

Sr.No. Methods Description
1dict.clear() Removes all elements of dictionary dict
2 dict.copy()Returns a shallow copy of dictionary dict
3dict.fromkeys()Create a new dictionary with keys from seq and values set to value.
4 dict.get(key, default=None) For "key" key, returns value or default if the key is not in the dictionary
5dict.has_key(key)Returns true if key in dictionary dict, false otherwise
6dict.items()Returns a list of dict's (key, value) tuple pairs
7dict.keys()Returns list of dictionary dict's keys
8dict.setdefault(key, default=None)Similar to get(), but will set dict[key]=default if "key" is not already in dict
9dict.update(dict2) Adds dictionary dict2's key-values pairs to dict
10dict.values() Returns list of dictionary dict's values

The power of Python language is undeniable when it comes to dictionaries and the tasks that can be achieved with it. Not only can it be used as a powerful tool for storing, retrieving, and manipulating data, but it also has some more advanced features that allow us to get even deeper into our research. With this newfound knowledge under your belt, you can now start building incredible projects utilizing all the advantages of Python dictionaries to their fullest extent with a Python training.
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like Java, Python, C, C++ etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like JavaScript and Cloud.

Accept cookies & close this