Convert a Python Dictionary to an Array

While working on a data project for a U.S.-based retail analytics company, I needed to convert a Python dictionary into an array for some quick numerical analysis using NumPy.

At first, it seemed simple; after all, a dictionary and an array both store data. But when I dug deeper, I realized that depending on what exactly I wanted (keys, values, or both), there were several efficient ways to handle this conversion.

In this tutorial, I’ll walk you through different methods to convert a Python dictionary to an array, step by step. I’ll also explain when to use each method and why.

What Is a Python Dictionary?

A Python dictionary is a collection of key-value pairs. It’s one of the most powerful and flexible data structures in Python, allowing you to map unique keys to specific values.

Here’s a quick example of a Python dictionary:

employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

In this dictionary, each employee’s name is a key, and their salary is the value. Now, let’s see how we can convert this dictionary into various types of arrays.

Method 1 – Convert Python Dictionary to an Array of Tuples Using items()

The simplest way to convert a Python dictionary to an array is by using the built-in items() method. This method returns a view object containing the dictionary’s key-value pairs as tuples.

Here’s how I do it in my projects:

employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert to array of tuples
employee_array = list(employee_data.items())

print(employee_array)

Output:

[('John', 85000), ('Emma', 92000), ('Liam', 78000), ('Olivia', 97000)]

You can refer to the screenshot below to see the output.

dict to array python

This method is quick and easy when you want both keys and values together. Each element in the resulting array is a tuple, which preserves the relationship between the key and its corresponding value.

Method 2 – Convert Python Dictionary Keys to an Array

Sometimes, you may only need the keys from a dictionary, for example, when you’re building a dropdown list of employee names in a web app.

Here’s how to extract just the keys as an array:

employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert keys to array
keys_array = list(employee_data.keys())

print(keys_array)

Output:

['John', 'Emma', 'Liam', 'Olivia']

You can refer to the screenshot below to see the output.

python convert dict to array

This approach is perfect when you only need to work with identifiers, such as usernames, product IDs, or state codes.

Method 3 – Convert Python Dictionary Values to an Array

If your focus is on the values (like salaries, prices, or scores), you can easily extract them using the values() method.

Here’s how:

employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert values to array
values_array = list(employee_data.values())

print(values_array)

Output:

[85000, 92000, 78000, 97000]

You can refer to the screenshot below to see the output.

convert dictionary to array python

This method is especially useful when performing numerical operations such as calculating averages or plotting data.

Method 4 – Convert Python Dictionary to NumPy Array

If you’re working with data analysis or machine learning, you’ll often need to convert your dictionary into a NumPy array for faster computation.

NumPy provides the array() function, which can convert lists or tuples into an array format that supports mathematical operations.

Here’s how to do it:

import numpy as np

employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert dictionary items to NumPy array
numpy_array = np.array(list(employee_data.items()))

print(numpy_array)

Output:

[['John' '85000']
 ['Emma' '92000']
 ['Liam' '78000']
 ['Olivia' '97000']]

You can refer to the screenshot below to see the output.

python dict to array

This method is ideal when you’re preparing data for numerical analysis or exporting it to a machine learning model.

You can also extract only the values as a NumPy array for calculations:

salary_array = np.array(list(employee_data.values()))

print(salary_array)

Output:

[85000 92000 78000 97000]

Now you can easily compute statistics like mean or standard deviation using NumPy functions.

Method 5 – Convert Python Dictionary to Array Using List Comprehension

List comprehension is one of my favorite Python features. It’s concise, readable, and powerful.

You can use it to convert a dictionary into an array in various ways. For example, let’s create an array of formatted strings from our dictionary:

# Python dictionary
employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert to array using list comprehension
formatted_array = [f"{key}: ${value}" for key, value in employee_data.items()]

print(formatted_array)

Output:

['John: $85000', 'Emma: $92000', 'Liam: $78000', 'Olivia: $97000']

This approach gives you full control over how you want to structure your array elements. It’s perfect when you need to prepare data for display, reporting, or exporting to a CSV file.

Method 6 – Convert Python Dictionary to Array Using zip()

The zip() function is another elegant way to convert a dictionary into an array of keys and values.

Here’s how I use it in real-world Python projects:

# Python dictionary
employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert dictionary to array using zip()
keys = list(employee_data.keys())
values = list(employee_data.values())

zipped_array = list(zip(keys, values))

print(zipped_array)

Output:

[('John', 85000), ('Emma', 92000), ('Liam', 78000), ('Olivia', 97000)]

This method is similar to items(), but it gives you more flexibility; for instance, you can modify the keys or values before zipping them together.

Method 7 – Convert Nested Python Dictionary to Array

Sometimes, you’ll encounter dictionaries within dictionaries (nested dictionaries). Let’s see how to flatten and convert them into an array.

# Nested dictionary
employee_data = {
    "Department A": {"John": 85000, "Emma": 92000},
    "Department B": {"Liam": 78000, "Olivia": 97000}
}

# Convert nested dictionary to array
nested_array = [(dept, emp, sal) for dept, data in employee_data.items() for emp, sal in data.items()]

print(nested_array)

Output:

[('Department A', 'John', 85000),
 ('Department A', 'Emma', 92000),
 ('Department B', 'Liam', 78000),
 ('Department B', 'Olivia', 97000)]

This approach is extremely useful when you’re working with hierarchical data, such as department-wise employee salaries or state-wise sales figures in a U.S. dataset.

Bonus Tip – Convert Python Dictionary to Array and Save as CSV

After converting your dictionary to an array, you might want to save it for later use. Here’s how to export the array to a CSV file using Python’s built-in csv module:

import csv

# Dictionary
employee_data = {
    "John": 85000,
    "Emma": 92000,
    "Liam": 78000,
    "Olivia": 97000
}

# Convert dictionary to array
employee_array = list(employee_data.items())

# Save to CSV
with open("employee_salaries.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Employee", "Salary"])
    writer.writerows(employee_array)

print("Data saved successfully to employee_salaries.csv")

This simple snippet helps you store your converted array for reports, dashboards, or data exchange.

Converting a Python dictionary to an array is a common and practical task, especially when dealing with data analysis, visualization, or machine learning workflows.

Whether you use items(), keys(), values(), numpy.array(), or even list comprehension, the right method depends on your specific use case.

Personally, I prefer NumPy arrays when working with numerical data and list comprehension when I need flexibility in formatting.

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.