How to Convert a Dictionary to an Array in Python?

In this tutorial, I will explain how to convert a dictionary to an array in Python. I had this requirement while working with data in Python for one of our USA clients. I will cover different methods to achieve this conversion and provide examples and screenshots of executed example code.

Dictionaries and Arrays in Python

Before we get into the conversion process, let’s briefly discuss what dictionaries and arrays are in Python.

Python dictionary is a collection of key-value pairs, where each key is unique. It allows you to store and retrieve data based on the keys. For example:

person = {"name": "John Doe", "age": 35, "city": "New York"}

On the other hand, an array is a collection of elements, typically of the same data type, stored in contiguous memory locations. In Python, we can use lists or the NumPy library to work with arrays. For example:

numbers = [1, 2, 3, 4, 5]

Read How to Read a Binary File into a Byte Array in Python

Convert a Dictionary to an Array in Python

Now, let’s explore different ways to convert a dictionary to an array in Python.

Method 1: Use list() and dict.values()

One simple approach is to use the list() function in combination with the values() method of the dictionary. This will create a list (array) containing only the values of the dictionary. Python provides the list() function to convert various data types, including dictionaries, to lists.

Example:

person = {"name": "Sarah Johnson", "age": 28, "city": "Los Angeles"}
person_array = list(person.values())
print(person_array)

Output:

['Sarah Johnson', 28, 'Los Angeles']

You can see the executed example code in the below screenshot.

Convert a Dictionary to an Array in Python

Read How to Create Arrays in Python

Method 2: Use NumPy.array() Function

If you need to work with the dictionary data using NumPy, you can directly convert the dictionary to a NumPy array using the numpy.array() function.

Example:

import numpy as np

scores = {"math": 85, "science": 92, "english": 88}
scores_array = np.array(list(scores.values()))
print(scores_array)

Output:

[85 92 88]

You can see the executed example code in the below screenshot.

How to Convert a Dictionary to an Array in Python

Read How to Find the Maximum Value in Python Using the max() Function

Method 3: Use Dictionary Comprehension and NumPy.array()

Another approach is to use dictionary comprehension to extract the values from the dictionary and then convert the result to a NumPy array.

Example:

import numpy as np

student = {"name": "Michael Smith", "grades": [85, 92, 88]}
grades_array = np.array([value for key, value in student.items() if key == "grades"][0])
print(grades_array)

Output:

[85 92 88]

You can see the executed example code in the below screenshot.

Convert a Dictionary to an Array in Python Use Dictionary Comprehension

Read How to Find the Index of an Element in an Array in Python

Handle Complex Python Dictionaries

When dealing with more complex dictionaries, such as those containing nested dictionaries or lists, you may need to apply additional techniques to convert them to arrays.

Example: Converting a Dictionary with Nested Lists to an Array

Suppose you have a dictionary representing a student’s grades for different subjects, where each subject has multiple test scores.

student_grades = {
    "math": [85, 92, 88],
    "science": [90, 87, 95],
    "english": [92, 88, 90]
}

To convert this dictionary to an array, you can use a combination of dictionary comprehension and NumPy’s array() function.

import numpy as np

grades_array = np.array([scores for subject, scores in student_grades.items()])
print(grades_array)

Output:

[[85 92 88]
[90 87 95]
[92 88 90]]

This code extracts the scores for each subject using dictionary comprehension and then creates a 2D NumPy array from the resulting list of lists.

Read How to Remove Duplicates from a Sorted Array in Python

Conclusion

In this tutorial, I explained different methods to convert a dictionary to an array in Python. If you have a simple dictionary and only need the values, use list() and dict.values() is an easy approach. If you need to work with the data using NumPy, converting the dictionary directly to a NumPyray numpy.array(), use dictionary comprehension and NumPy.array(). We also discussed how to handle complex Python dictionaries.

You may also like to 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.