How To Convert a Dictionary to a List in Python – Definitive Guide

Python dictionary allows you to store items as a key-value pair.

You can convert a dictionary to a list in Python using the list(yourdict.items()) method.

This tutorial teaches you how to convert a dictionary to a list in python. You can convert dictionary keys or values to a list.

If You’re in Hurry…

You can use the dict.items() method to convert the dictionary into a list of tuples. The items() method returns a new view of the dictionary items as (key,value) pairs.

When you pass the items() view to the list() constructor, you’ll get a list of tuples that contains the dictionary items.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

list(yourdict.items())

Output

[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('fourr', 4)]

If You Want to Understand Details, Read on…

Different methods are available to

  • convert dictionary items to a list of tuples
  • convert dictionary keys to a list
  • convert dictionary values to a list.

Convert Dictionary to List Of Tuples

You can convert a dictionary to a list of tuples using the list(dict.items()) method.

The dict.items() method returns a new view of the dictionary items as tuples. Each tuple contains a key and the value of the dictionary. For example, (one,1). One tuple represents one entry of the dictionary.

When you pass these tuples to the list() constructor, the dictionary items are converted into a list of tuples.

Code

The following code demonstrates how to convert a dictionary to a list of tuples.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

list(yourdict.items())

Output

[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('fourr', 4)]

Convert Dictionary Keys to List

You can use the dict.keys() method to convert dictionary keys to a list.

The keys() method returns a view of the dictionary keys. When you pass the view to the list() constructor, you’ll get a list of keys from the dictionary.

You can use this method when you just want to get the keys as a list.

Code

The following code demonstrates how to convert the dictionary keys to a list.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

list(yourdict.keys())

Output

    ['one', 'two', 'three', 'four', 'fourr']

Convert Dictionary Values To List

You can use the dict.values() method to convert dictionary values to a list.

The values() method returns a view of the dictionary values. When you pass the values view to the list() constructor, you’ll get a list of values from the dictionary.

You can use this method when you just want to get the values as a list.

Code

The following code demonstrates how to convert the dictionary values to a list.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

list(yourdict.values())

Output

    [1, 2, 3, 4, 4]

Using List Comprehension and Dict.items

List comprehension is a simpler syntax to iterate over an iterable and create a list.

You can use the list comprehension and dict.items() method to convert dictionary items to a list.

You can also use list comprehension to convert keys or values alone to a list.

You can use this method when you want to convert only specific keys or values to a list by specifying the condition in the list comprehension.

Code

The following code demonstrates how to use list comprehension to convert the complete dictionary items to a list. To convert specific items, see the next example.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

lst = [(k, v) for k, v in yourdict.items()] 

lst

Output

All the items in the dictionary are converted to a list of tuples.

    [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('fourr', 4)]

Converting Specific Dictionary Items to a List

To convert specific items of a dictionary to a list, you can specify the condition in the if statement on the list comprehension.

Code

The following code demonstrates converting items with a value greater than two to a list.
This condition is specified in the if statement.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

lst = [(k, v) for k, v in yourdict.items() if v>2] 

lst

Output

The dictionary items with a value greater than two are converted as a list of tuples.

[('three', 3), ('four', 4), ('fourr', 4)]

Using Zip With Dict.keys and Dict.values

The zip function produces tuples with items from each iterable you pass.

Here you can pass the dict.keys() and dict.values() as the iterables to zip it together and create a tuple.

It also accepts another parameter called strict=False. It allows you to check the length of the iterable. If this parameter is set to True, a ValueError will be thrown when the length of the two tables is not equal.

You can use this method to get the items as a list and when any of the dictionary keys doesn’t contain any value.

Code

The following code demonstrates how to use the zip() function to convert dictionary items to a list of tuples.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "fourr": 4
}

zip_items = zip(yourdict.keys(), yourdict.values()) 

lst = list(zip_items)

lst

Output

    [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('fourr', 4)]

Conclusion

You’ve learned how to convert a dictionary to a list in Python.
Using various methods, you’ve also learned how to convert the dictionary items(Keys and values), only keys, or only values to a list in Python.

Additionally, you’ve learned how to use list comprehension to convert specific items to a list.

You May Also Like

Leave a Comment