Python Dictionary Create, Add, Delete, Looping with Examples

Create your dictionary variable in Python with simple methods given here. You can perform various operations with Python dictionary variable. These operations contain accessing, looping over elements and other useful operations.

You can get the keys and the elements dictionary related to these keys using Python.

What is Dictionary Variable

The dictionary variable contains the collection of unordered elements. The dictionary variable contains the elements enclosed within the curly brackets({}). The list variable also enclosed within the curly brackets. However, the dictionary variable contains the keys and the elements.

The dictionary variable is changeable and indexed. It can contain a combination of elements with string or integers. You can change or remove each element of the dictionary using Python. The dictionary contains keys and each key has its related elements.

Python dictionary variable create, add, update, delete and perform iteration

Create Dictionary Variable Using Python

To create your dictionary variable in Python, you have to use the curly brackets({}) to place the elements. Each element contains keys and its matched element. The elements are indexed and start from index zero(0).

You have to place dictionary variable in comma separation. Use the colon(:) between the keys and it’s matching element to create the dictionary variable in Python.

Output

{‘one’: ‘Bill’, ‘two’: 18, ‘three’: ‘Smith’, ‘four’: ‘Dara’, ‘five’: 21, ‘six’: ‘Wilson’}

The above example contains the dictionary variable with 6 elements. The output contains all the elements enclosed within the curly brackets. However, you can access only the required elements of a dictionary with the below-given method.

Access Items of Dictionary Variable in Python

The dictionary variable in Python works like an associated array in other programming languages. The elements are indexed with the key elements.

To access the elements of the dictionary variable, you have to use the index operator([]). The index operator requires one argument as the key position of the elements. Use the method as given below to get the element of the dictionary in Python.

Output

Bill
18

The above example access the first and the second element of the dictionary variable. ‘one’ and ‘two’ are the keys for the element which you can use to get the required elements.

Add Items in Dictionary Variable in Python

If you want to add new items to the dictionary using Python. You have to use a new index key and assign a new value to it. You can use any new key for adding the new element to the dictionary. Each time you create and assign new key and value, it gets appended to the end of the dictionary in Python.

Output

{‘one’: ‘Bill’, ‘two’: 18, ‘three’: ‘Smith’, ‘four’: ‘Dara’, ‘five’: 21, ‘six’: ‘Wilson’, ‘seven’: 32}

The above example showing the dictionary with the added elements. Previously there are six elements but after the addition, you will get seven elements in the output.

If you don’t know the size of the dictionary after addition of the new element, you can use the below example to find the length of the dictionary.

Find Length of Dictionary in Python

The size of the dictionary variable is the number of elements contained in it. You can get or find the length of the dictionary using the len() function. Pass the dictionary variable as the argument of the len function. See the example below to get the length of your dictionary variable in Python.

Output

6

The above example showing the length of the dictionary as 6. There are 6 elements and each element contains the keys with its values.

Change Items of Dictionary

In addition to adding the new element to the dictionary, you can also change the elements. To change the elements of the dictionary variable, you have to use the method given below. The method assigns the new values to the old keys to change the element values.

Output

{‘one’: ‘Chinu’, ‘two’: 18, ‘three’: ‘Smith’, ‘four’: ‘Dara’, ‘five’: ‘Champ’, ‘six’: ‘Wilson’}

The above example showing the dictionary with the changes elements. The first and the fifth elements of the dictionary variable contains the new elements. The output showing the changed elements with the other old elements of the dictionary.

Delete Dictionary Items Using Python

In addition to adding the element to the dictionary in Python. You can also perform deletion operation with the dictionary. To delete any dictionary element, you have to use the pop() function of Python.

The delete operation deletes the elements you want with its key and the value. You can delete only the single element at one time using pop() function. To delete more elements of the dictionary, you have to use the pop() again and again.

Output

18
21
{‘one’: ‘Bill’, ‘three’: ‘Smith’, ‘four’: ‘Dara’, ‘six’: ‘Wilson’}

The above example showing two elements which you delete using pop(). After that, it prints the dictionary after each deletion. Before deletion, the dictionary contains 6 elements. However, after deletion, it results out 4 elements.

Perform Iteration or Loop Through Dictionary Elements.

If you want to perform iteration over the dictionary. You have to use the below-given method. There are six elements in the dictionary which gives iteration 6 times.

Output

Bill
18
Smith
Dara
21
Wilson

The above prints all the elements of the dictionary in the output after iteration. The elements are printed with a single element in a single line.

You may also like to read

Hope, you like this post of How to create Python dictionary variable. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to create your Python dictionary.

Join the Conversation

1 Comment

  1. The use of semi-colons at the end of each statement is truly archaic and needless Python syntax.That was just a hang-over from its early ‘C’-oriented days and has no place in the modern world.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.