Python Dictionary Operations

Date: 19/09/2019

In this tutorial, I going to explain the basics of python dictionary operations and methods.

INTRODUCTION

Python dictionary is an unordered collection of items and also it contains key-value pairs and enclosed by curly-braces { }.

Each key-value pair in a python dictionary is separated by a colon(:) and each key is separated by a ‘comma’.

Most importantly python dictionary, keys should not be repeated and values can be any datatype.

Create an empty dictionary:

First of all we can create the dictionary using the variable name for that we can use empty curly-braces. It will create an empty dictionary in python.

Create a dictionary with the element:

we can add items inside curly braces {} and separated by a comma for creating a dictionary with element.

Access elements from the dictionary:

We can access the dictionary elements using the key.

Update dictionary value:

We can add new items into the dictionary and also we can update the existing item.

Add element into the dictionary:

We can add the new element into the existing dictionary with the new key-value pair.

Remove element from the dictionary:

We can remove the element from the dictionary using the pop() method, for that, we can give key name within the pop method.

Remove all item from the dictionary:

We can use clear() method to remove all element from the dictionary. It will display the empty dictionary.

Delete dictionary:

We can use the del method to delete the full dictionary.

Thanks for using Pheonix solutions.

You find this tutorial helpful? Share with your friends.

Leave a Reply