Convert Dictionary to List in Python Tutorial Example

13-Feb-2023

.

Admin

Convert Dictionary to List in Python Tutorial Example

Hi Dev,

In this tute, we will discuss convert dictionary to list in python tutorial example. I would like to share with you python dictionary into list. We will use python convert dict object to list. We will look at example of how to convert dict to list in python. Let's get started with how to convert dictionary to list in python.

There are several ways to convert a python dictionary to list. I will give you the following three examples to convert a dictionary to list in python.

Example 1: Python Convert Dictionary Items to List


main.py

myDictionary = {

"id": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Python Convert Dictionary Items to List

newDictionary = list(myDictionary.items())

print(newDictionary)

Output:

[('id', 1), ('name', 'Piyush Kamani'), ('email', 'piyush@gmail.com')]

Example 2: Python Convert Dictionary Values to List

main.py

myDictionary = {

"id": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Python Convert Dictionary Values to List

newDictionary = list(myDictionary.values())

print(newDictionary)

Output:

[1, 'Piyush Kamani', 'piyush@gmail.com']

Example 3: Python Convert Dictionary Keys to List

main.py

myDictionary = {

"id": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Python Convert Dictionary Keys to List

newDictionary = list(myDictionary.keys())

print(newDictionary)

Output:

['id', 'name', 'email']

#Python