Understanding Python Dictionary

Python has multiple data structures, and Dictionary is one of them. This tutorial will take you through every aspect of the dictionary data structure in Python language.

Advertisements

What is a Dictionary in Python?

When Python was launched, Dictionary was introduced as an unordered collection of items. However, later with Python 3.7 launch, Dictionary was made an ordered collection of items.

Dictionary is somewhat similar to an array, but every element of the dictionary has a key linked. You can use that key to access its respective element, unlike an array.

Note: The key of a dictionary has to be unique. Let’s understand the syntax of a Dictionary:

Syntax

Every element in a dictionary is comma-separated, and all the elements with their keys are enclosed in the curly brackets. The syntax is as follows:

Dic = {"key1":"value","key2":"value"}

Example: One real example of the dictionary is to store the number of subjects every student is enrolled in:

SubRecord = {"Smith":5,"Sam":4,"Lucy":6}

Here “SubRecord” is the name associated with the dictionary, and “Smith,” “Sam,” and “Lucy” are the keys of the dictionary. Whereas the integer values represent the number of subjects students are enrolled in.

How to define a Dictionary?

Though the syntax might seem simple to you, but there are multiple ways to define a dictionary in Python.  In this section, we will learn about some of the ways to create a dictionary in a Python program:

Basic Implementation

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}
print(type(Dict1))

Output:

<class 'dict'>

Using “Dict()” Method with Assigning Operator

In the above method, we have used the colon “:” to link every element and key. It is preferred when we have to set integers as keys. However, if you want to use string instead of the colon, “:” equals to “=” can also be used. 

Dict2 = dict(
First =&nbsp; 'Python',
Second = "Programming"
print(type(Dict2))

Output:

<class 'dict'>

Apart from these two, there could be another way to create/define a dictionary in Python. We have listed the most popular ones among all. 

Accessing Dictionary Elements

Accessing Dictionary elements is pretty straightforward. It is almost similar to accessing elements of an array. In Array, we use the index values to access the elements, whereas, in Dictionary, we have to use keys to access the elements.

For instance,
We have the following dictionary:

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}

Now, if you want to access the 4th element i.e., “Programming,” you have to write a code statement like this:

print(Dict1[4])

Output:

Programming

In case you provide a key input that doesn’t exist; you will get a similar exception message:

Traceback (Most recent call last):
  File '<string>', line 5, in <module>
KeyError: 5
>

Adding and Removing Elements from Dictionary

To add elements in a dictionary, you can use multiple ways. A few of them are listed below:

Adding Elements directly using Key

In this method, we simply add an element by defining it with a unique key.

Advertisements

For instance,

Let’s assume we have a pre-existing dictionary with the following elements:

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}

Now, add a new element, we can use a similar code statement:

Dict1[5] = "Tutorial"
print(Dict1)

Output:

{1:"This",2:"is",3:"Python",4:"Programming,5:Tutorial"}

Here we have used a unique key; however, its value gets updated if an existing key is used.

For instance,

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}
	Dict1[4] = "Tutorial"
	print(Dict1)

Output:

{1:"This",2:"is",3:"Python",4:"Tutorial"}

Adding Element using Update() Method

Python also has a built-in method to Add or Update existing dictionary elements.

For Instance,

Adding an new element:

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}
Dict1.update({5:"Tutorial"})
print(Dict1)

Output:

{1:"This",2:"is",3:"Python",4:"Programming,5:Tutorial"}

Updating a existing element:

Dict1 = {1:"This",2:"is",3:"Python",4:"Programming"}
Dict1.update({3:"a"})
print(Dict1)

Output:

{1:"This",2:"is",3:"a",4:"Programming"}

So, that’s how you can add or update elements in the Python dictionary using the update() method. Now, lets learn:

Removing an Element from Dictionary

To remove an element from a dictionary, you can use the built-in del keyword of python.

For instance,

Dict1 = {1: 'This', 2: 'is', 3: 'Python', 4: 'Programming', 5: 'Tutorial'}
del Dict1[5]
print(Dict1)

Output:

{1:"This",2:"is",3:"Python",4:"Programming"}

Using the del keyword, you can also delete the whole dictionary.

Final Notes

Since you have gone through the whole tutorial, we hope now you have a clear understanding of Dictionaries in Python. In the next article, you will learn about other python data structures.

Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version