Dict To list – Convert A Dictionary to a List in Python

Do you want to convert the Dictionary to a List in Python? This article will show how to convert a dictionary to a list in Python.

You have a dictionary in Python and you want to either convert the values of the dictionary in the list or you want to convert the keys to a list. You can do this conversion in 5 different ways that are discussed below.

Dict To List – Using items Method of Dictionary Class

In the dict class of Python, there is a method or function named as items. Once this method is used on the existing dictionary it returns the list of the key-value pair of Dictionary in Python.

Let us see in the below example code the usage of items() on a dictionary to get the list of values in Python.

#Initializing Small List of Dictionary
dictNumber = {'C':3, 'A':1, 'B':2, 'D':4, 'E':5 }

#Converting the Dict to List using items
listOfKeysValues = list(dictNumber.items())

#Printing the List converted using Dictionary
print(listOfKeysValues)

#Printing Tuple
print(listOfKeysValues[0])

#Printing Specific Value
print(listOfKeysValues[0][1])

Output:

[('C', 3), ('A', 1), ('B', 2), ('D', 4), ('E', 5)]
('C', 3)
3

So using items in the dictionary I was able to get the dictionary converted completely to the list. But we have two items i.e. a tuple as each element of the list. Hence Now suppose you want to get only the list of values or list of keys of a given dictionary then follows the below approach.

Dict To list - Convert A Dictionary to a List in Python

Using values() To Get List Of Values from Dictionary in Python

You want to convert the values of the dictionary into the list then you need to use the values() method of the dictionary class that returns all the values present in the dictionary.

Let us see the below example code for usage of values method on the dictionary in Python.

#Initializing Small List of Dictionary
dictNumber = {'C':3, 'A':1, 'B':2, 'D':4, 'E':5 }

#Converting the Dict to List using values
listOfValues = list(dictNumber.values())

#Printing the List converted using Dictionary
print(listOfValues)

Output

[3, 1, 2, 4, 5]

Using the above code we were successfully able to print the list of values converted using the dictionary.

Using keys() Method to Convert the Dictionary keys into A List In Python

If you want to convert the keys of a dictionary into a list then you need to use the keys() method present in Dictionary Class. This method will return all the keys present in the Dictionary as a list.

Let us see in the below example code the usage of keys() to get a list of Keys from a dictionary.

#Initializing Small List of Dictionary
dictNumber = {'C':3, 'A':1, 'B':2, 'D':4, 'E':5 }

#Converting the Dict to List using keys
listOfKeys = list(dictNumber.keys())

#Printing the List converted using Dictionary
print(listOfKeys)

Output:

['C', 'A', 'B', 'D', 'E']

Using the above code you will be able to print out the list of keys present in the given dictionary easily. Also, those keys are present in form of a list and you can do further operations if you want to perform any.

Wrap Up

I hope you were able to get the solution to the problem of how to convert a dictionary into a list in Python. I have provided three different types of methods that you can use whichever suits you need.

Let me know if you have any better solution than the one listed above and are more efficient than the ones listed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Discord.py Bot Add Reaction To a Message Python
  2. Python Get The Minimum Value In Dictionary with Key
  3. How To Append Multiple Values To A List in Python
  4. How To Reboot or Restart Python Script
  5. Python: To Check If String Contains a Substring
  6. Python: How To Check If a String Contains a List of Substring

Leave a Comment