In this python article, We are going to learn next python Dictionary data type. What is dictionary data type, How to create it. How to add/edit/delete and access dictionary element. Different methods associated with python dictionary data type. So let’s get started with the python dictionary data type tutorial.

What is python dictionary data type:

Dictionary data type in Python is an unordered collection of data values. In simple words it is a collection of key:value pairs. Each key is mapped to its associated value. So the main purpose of dictionary is to store a value by associating some key to it and in future extract the value when key is provided as an input.

While in other python data types, Data is stored in a way that, a single value is considered as one element however in dictionary data type complete key:value pair is considered as one element. Dictionary is used to store data values like a map.

If you compare python dictionary data type with other programming languages you will find many similarities between dictionary and “associative array”. In some languages it is also called as “hashes” or “maps”. In short this data types is used to store the data in key:value pair format

Python Dictionary data type

How to create a dictionary in python:

A dictionary can be created by writing key:value pairs inside curly braces {}“. Each key value pair is separated by colon :” and each pair is separated by comma,“.

One thing to note here is, In a dictionary key must be unique and should belong to any immutable data type (string, Number, Tuple etc.)

Example: Create a dictionary with students data. Name of student as a key and role number as a value.

# Create a dictionary in python.
school_dictionary = {'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}
print(school_dictionary)

#Output:
{'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}

How to access python dictionary element:

Dictionary elements can be accessed by specifying a key within the square brackets [].

As we have know, For other data types we use index number to access an element. So in case of dictionary data type instead of using index numbers we are going to use key to access the element.

There is one more way to access the dictionary element is by using python built-in get() method. In get() method specify the key and it will return us the value associated with it. Advantage of using get() method is that, In case the specified key is not found then it returns “none” instead of “keyerror“.

Example: Python program to access a directory element and print it.

# Access and print single dictionary element.
school_dictionary = {'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}
print(school_dictionary['john'])

# Output:
10

How to add/append new element into a dictionary:

Dictionary is a mutable data type i.e We can add/append new elements or edit existing element after its creation.

Syntax to add/append new key:value pair into existing dictionary is as below;

Dictionary_Name['New_key'] = value
# Add new element into a dictionary.
school_dictionary = {'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}
school_dictionary['New_Element']=50
print(school_dictionary)

# Output:
{'john': 10, 'Alex': 18, 'smith': 25, 'Harry': 32, 'New_Element': 50}

Update dictionary element:

While adding a element, If the specified key is already used in the dictionary then its value gets updated otherwise new key:value pair gets added into the dictionary. So while updating the exiting element, Provide its key with updated value in following syntax.

Dictionary_Name['Existing_key'] = value

# Create a dictionary in python.
school_dictionary = {'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}
print(school_dictionary)

#Output:
{'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}

# Update the existing dictionary value.
school_dictionary['Alex'] = 100
print(school_dictionary)

# Latest updated dictionary Output:
{'john':10, 'Ben':15, 'Alex':100, 'smith':25, 'Harry':32}

How to delete/remove directory element:

There are various methods available for deleting the directory elements. Let’s see each of them;

  1. del: Using this keyword we can delete specified key:value pair from dictionary. Here we need to provide the input key which we want to delete in square brackets.
  2. clear(): Using this method we can clear complete dictionary and make it empty. No input is required to this method since it removes all the directory elements.
  3. pop(): This method removes the item with specified key and returns us value. Here we need to specify the key which want to remove.
  4. popitem(): This method removes the first item from the dictionary and returns us the complete key:value pair. Here no input is required since it removes the first item of the dictionary.

Please go through the example below, Here we have created on dictionary of school students in such a way that name of each student is a key and their role number is a value. We are going to perform dictionary remove/delete operation on dictionary.

# Delete an element from dictionary using "del" keyword.
school_dictionary = {'john':10, 'Ben':15, 'Alex':18, 'smith':25, 'Harry':32}
del school_dictionary['Ben']print(school_dictionary)

#Output
{'john': 10, 'Alex': 18, 'smith': 25, 'Harry': 32}

# Remove an element from dictionary using "pop()" method.
print(school_dictionary.pop('Alex'))

#Output
18

# Remove first element of dictionary using "popitem()" method.
print(school_dictionary.popitem())

#Output
('Harry', 32)

# Remove all the dictionary elements using "clear()" method.
school_dictionary.clear()
print(school_dictionary)

#Output
{}

 


Continue Learning:

Also you can get more help on python forum.  Click Here

Share this Post

Leave a Comment

Your email address will not be published.