Loading...

Python Dictionary Methods

What are Python Dictionary? What are the various Python dictionary methods?

This blog gives in-depth knowledge of Python Dictionary Methods. Read our blog to learn more...

Python Dictionary Methods

Sep 25, 2022    By Team YoungWonks *

What is a Python Dictionary?

The Python Dictionary is a data structure that stores data as key-value pairs. Dictionaries, as opposed to Python lists, are a collection of unique keys associated with values. It doesn't keep duplicate keys. A Python dictionary's values are mapped using keys. The key-value pairs contain data of the specified data type, such as integers, strings, and so on. This data structure is also found in other programming languages, such as Java, and is known technically as a hash map.

In this Python tutorial, we will learn how to manipulate key-value pairs using Python Dictionary methods.

 

Python Dictionary Methods

There are several methods for manipulating or extracting data from a Python dictionary. We'll also learn a few built-in methods for changing the dictionary. Let us now learn these methods.

1. Create a Python Dictionary

Curly braces ({}) are used to create an empty dictionary, which contains key-value pairs separated by commas (,). A colon separates the key and values.

Syntax: dict = {}

python dictionary methods create

2. Add key-value pairs into a dictionary

Using the syntax shown below, we can add new key-value pairs to a dictionary. The same value can be assigned to multiple keys.

Syntax: dict[new key] = value

Alternate Methods:

Syntax: dict.update( {new key:value}

We can use the built-in function described above to add new dictionary key-value pairs to a dictionary.

python dictionary methods add key-value pairs

3. Delete key-values pairs into a dictionary

Key-value pairs can be removed from a dictionary.

a. Method 1: Removing the key automatically removes the values from a dictionary.

Syntax: del (dict[key])

python dictionary methods delete

b. Method 2: This method removes given key-value pairs.

Syntax: dict.pop(key)

python dictionary methods remove

c. Method 3: This method removes the last item from a dictionary.

Syntax: dict.popitem() 

python dictionary methods popitem

4. Update Dictionary values

Since dictionary is unique to keys we can only update the value for the keys.

Syntax: dict[key] = new value

python dictionary methods update

5. Get Values of specified keys

We can use the get method to retrieve the corresponding value of the specified key. Here, we pass the key as a parameter to the function, which return value.

Syntax: dict.get(dictionary key)

python dictionary methods get value

6. Duplicate a dictionary

To create a copy of the dictionary, we can use the method given below.

Syntax: dict1 = dict.copy()

python dictionary methods duplicate

7. Get a list of keys and list of values separately

We can segregate the keys and values in a list format.

Syntax: dict1 = dict.keys()

            dict2 = dict.values()

python dictionary methods list of keys

python dictionary methods list values

8. Get key-value pairs in a list format

This built-in method is used to convert a dictionary into a list of key-value tuple pairs. This method returns a dict items object that cannot be subscripted; iterating over it will result in a keyerror.

In order to get each tuple containing the key-value pairs, we have to use the built-in-list method to convert it into a list.

Syntax: dict1 = dict.items()

python dictionary methods items tuple

9. Iterate on a dictionary

We can run a for loop on a dictionary to obtain each key and value one by one.

Syntax: for i in dict:

                  print (i, dict[i])

python dictionary methods loop;

Create a dictionary using a tuple.

Syntax: keys = (key1, key2, key3)

            values = (value1, value2, value3)

            dict.fromkeys(keys, values)

This method returns a dictionary having specified keys and values.

10. Empty a dictionary

We use the clear method to empty a dictionary.

Syntax: dict.clear()

python dictionary methods empty

11. Get values of specified key using the setdefault method

In this method, if the item is present in the dictionary it returns its value. Else, it returns a dictionary with new key and default value.

Syntax: dict.setdefault(key, value)

 

Summary

In this blog, we learned about all the Python dictionary methods. You can now incorporate these into your programmes. In a future blog, we will go over the Python random module in depth.

*Contributors: Written by Aayushi Jayaswal; Lead image by Shivendra Singh

This blog is presented to you by YoungWonks. The leading coding program for kids and teens.

YoungWonks offers instructor led one-on-one online classes and in-person classes with 4:1 student teacher ratio.

Sign up for a free trial class by filling out the form below:



By clicking the "Submit" button above, you agree to the privacy policy
help