How to Convert a Dictionary to a List in Python?

Recently, in a Python webinar, someone asked me a question about covering a dictionary to list. After researching and testing, I found a few effective ways to accomplish this task. In this tutorial, I will explain how to convert a dictionary to a list in Python with suitable examples and screenshots.

Dictionaries in Python

Before getting into the conversion process, it’s important to understand what a dictionary is in Python. A dictionary is an unordered collection of items that store data in key-value pairs. For instance, consider the following dictionary that holds information about some popular American cities:

cities = {
    'New York': 'NY',
    'Los Angeles': 'CA',
    'Chicago': 'IL',
    'Houston': 'TX',
    'Phoenix': 'AZ'
}

In this example, the city names serve as keys, while their corresponding state abbreviations are the values.

Read How to Randomly Select from a List in Python?

Convert a Dictionary to a List in Python

There are several ways to convert a dictionary to a list in Python. Below, I will outline the most common methods, complete with examples.

1. Convert Keys to a List

If you only need the keys from a Python dictionary, you can easily convert them to a list using the keys() method. Here’s how:

city_keys = list(cities.keys())
print(city_keys)

Output:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can see the output in the screenshot below.

Convert a Dictionary to a List in Python

Check out How to Get the Index of an Element in a List in Python?

2. Convert Values to a List

In some cases, you may want to extract just the values. You can achieve this using the Python values() following method:

city_values = list(cities.values())
print(city_values)

Output:

['NY', 'CA', 'IL', 'TX', 'AZ']

You can see the output in the screenshot below.

How to Convert a Dictionary to a List in Python

Read How to Merge Lists Without Duplicates in Python?

3. Convert Key-Value Pairs to a List of Tuples

If you want to retain both keys and values, you can convert the dictionary into a list of tuples using the items() method in Python. This method is particularly useful when you need to process both the keys and values together.

city_items = list(cities.items())
print(city_items)

Output:

[('New York', 'NY'), ('Los Angeles', 'CA'), ('Chicago', 'IL'), ('Houston', 'TX'), ('Phoenix', 'AZ')]

You can see the output in the screenshot below.

Convert a Dictionary to a List in Python Key-Value Pairs to a List of Tuples

Check out How to Convert String to List in Python?

4. Use List Comprehensions

Python list comprehensions provide a concise way to create lists. You can use them to convert dictionary items into a list format that suits your needs. Here are some examples:

Example: List of City Names

city_list = [city for city in cities]
print(city_list)

Output:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

Example: List of State Abbreviations

state_list = [state for state in cities.values()]
print(state_list)

Output:

['NY', 'CA', 'IL', 'TX', 'AZ']

Read Convert String to List in Python Without Using Split

5. Create a List of Custom Objects

Sometimes, you might want to convert a dictionary to a list in Python of custom objects. For instance, if you have a dictionary of cities with their populations, you can create a list of city objects.

class City:
    def __init__(self, name, state, population):
        self.name = name
        self.state = state
        self.population = population

cities_with_population = {
    'New York': {'state': 'NY', 'population': 8419600},
    'Los Angeles': {'state': 'CA', 'population': 3980400},
    'Chicago': {'state': 'IL', 'population': 2716000},
}

city_objects = [
    City(name, info['state'], info['population'])
    for name, info in cities_with_population.items()
]

for city in city_objects:
    print(f"{city.name}, {city.state}: {city.population}")

Output:

New York, NY: 8419600
Los Angeles, CA: 3980400
Chicago, IL: 2716000

Check out How to Iterate Through a List in Python?

Handle Nested Dictionaries

When dealing with nested dictionaries, the conversion process may require a bit more complexity. For example, consider a dictionary that contains information about various cities, including their neighborhoods:

city_data = {
    'New York': {
        'state': 'NY',
        'neighborhoods': ['Manhattan', 'Brooklyn', 'Queens']
    },
    'Los Angeles': {
        'state': 'CA',
        'neighborhoods': ['Hollywood', 'Beverly Hills', 'Santa Monica']
    }
}

To convert this nested dictionary into a list of neighborhoods, you can use a nested list comprehension:

neighborhoods = [
    neighborhood 
    for city in city_data.values() 
    for neighborhood in city['neighborhoods']
]
print(neighborhoods)

Output:

['Manhattan', 'Brooklyn', 'Queens', 'Hollywood', 'Beverly Hills', 'Santa Monica']

Check out How to Write a List to a File in Python?

Conclusion

In this article, I explained how to convert a dictionary to a list in Python. I discussed converting keys to a list , converting values to a list , converting key-value pairs to a list of tuples, using list comprehension, and creating a list of custom objects. I also covered how to handle nested dictionaries.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.