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.

Basic Example

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)]

This tutorial teaches you how to convert a dictionary to a list in python. You can convert dictionary keys or 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 in the dictionary.

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

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)]

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.
  • Pass the view to the list() constructor, and 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

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.
  • Pass the values view to the list() constructor, and 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

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.

  • 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.

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

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,

  • 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.
  • The 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.

  • Pass the dict.keys() and dict.values() as the iterables to zip it together and create a tuple.

The zip() accepts another parameter.

  • another parameter is 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.

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

Code

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)]

Additional Resources

Leave a Comment