Skip to main content

Python NumPy: How to Convert a Dictionary to a NumPy Array in Python

Converting a Python dictionary to a NumPy array is a common task when you need to transition from key-value data structures to numerical arrays for mathematical computations, data analysis, or machine learning. NumPy arrays offer significant performance advantages over dictionaries for numerical operations, and understanding how to bridge these two data structures is essential.

In this guide, you'll learn multiple methods to convert dictionaries to NumPy arrays, handle different data types, and choose the right approach for your specific use case.

Basic Conversion Using np.array()

The most straightforward approach is to extract the dictionary's key-value pairs with dict.items(), convert them to a list, and pass the list to np.array().

import numpy as np

data = {1: 'tutorial', 2: 'reference', 3: '.com'}

# Convert to list of key-value pairs, then to NumPy array
pairs = list(data.items())
arr = np.array(pairs)

print(arr)
print(f"Shape: {arr.shape}")
print(f"Dtype: {arr.dtype}")

Output:

[['1' 'tutorial']
['2' 'reference']
['3' '.com']]
Shape: (3, 2)
Dtype: <U21

Step-by-step breakdown:

  1. data.items() returns key-value pairs as tuples: [(1, 'tutorial'), (2, 'reference'), (3, '.com')].
  2. list() converts the view object to a list.
  3. np.array() creates a 2D array where each row is a key-value pair.
note

Notice that the integer keys were converted to strings ('1', '2', '3'). NumPy arrays require homogeneous data types, so when keys and values have different types, NumPy casts everything to a common type - usually strings.

Converting Only Keys or Values

Often, you don't need both keys and values in the array. You can convert them separately:

import numpy as np

data = {'a': 10, 'b': 20, 'c': 30, 'd': 40}

# Convert only keys
keys_array = np.array(list(data.keys()))
print(f"Keys: {keys_array}")

# Convert only values
values_array = np.array(list(data.values()))
print(f"Values: {values_array}")
print(f"Dtype: {values_array.dtype}")

Output:

Keys:   ['a' 'b' 'c' 'd']
Values: [10 20 30 40]
Dtype: int64

Converting only the values is particularly useful when you need a numeric array for mathematical operations:

import numpy as np

scores = {'math': 92, 'science': 88, 'english': 95, 'history': 78}

values = np.array(list(scores.values()))

print(f"Scores: {values}")
print(f"Mean: {values.mean():.2f}")
print(f"Max: {values.max()}")
print(f"Std: {values.std():.2f}")

Output:

Scores: [92 88 95 78]
Mean: 88.25
Max: 95
Std: 6.42

Converting a Numeric Dictionary to a 2D Array

When both keys and values are numeric, you get a clean numeric array:

import numpy as np

data = {1: 100, 2: 200, 3: 300, 4: 400}

arr = np.array(list(data.items()))

print(arr)
print(f"Shape: {arr.shape}")
print(f"Dtype: {arr.dtype}")

# Now you can perform numeric operations
print(f"\nColumn sums: {arr.sum(axis=0)}")
print(f"Row sums: {arr.sum(axis=1)}")

Output:

[[  1 100]
[ 2 200]
[ 3 300]
[ 4 400]]
Shape: (4, 2)
Dtype: int64

Column sums: [ 10 1000]
Row sums: [101 202 303 404]

Using Structured Arrays for Mixed Types

When your dictionary has different data types for keys and values (e.g., string keys and numeric values), a structured array preserves type information instead of converting everything to strings:

import numpy as np

data = {'Alice': 92.5, 'Bob': 88.0, 'Charlie': 95.3}

# Define a structured dtype
dtype = np.dtype([('name', 'U10'), ('score', 'f8')])

# Create structured array
arr = np.array(list(data.items()), dtype=dtype)

print(arr)
print(f"\nNames: {arr['name']}")
print(f"Scores: {arr['score']}")
print(f"Mean score: {arr['score'].mean():.2f}")

Output:

[('Alice', 92.5) ('Bob', 88. ) ('Charlie', 95.3)]

Names: ['Alice' 'Bob' 'Charlie']
Scores: [92.5 88. 95.3]
Mean score: 91.93
tip

Structured arrays let you access columns by name (like arr['score']) while maintaining the correct data type for each field. This is ideal when you need both string and numeric data in a single array.

Converting a Dictionary of Lists to a 2D Array

A common pattern is a dictionary where each value is a list of numbers, representing columns of data:

import numpy as np

data = {
'temperature': [72, 75, 68, 71],
'humidity': [45, 50, 55, 48],
'pressure': [1013, 1010, 1015, 1012]
}

# Stack values as columns
arr = np.column_stack(list(data.values()))

print("Array:")
print(arr)
print(f"\nShape: {arr.shape}")
print(f"Column names: {list(data.keys())}")

Output:

Array:
[[ 72 45 1013]
[ 75 50 1010]
[ 68 55 1015]
[ 71 48 1012]]

Shape: (4, 3)
Column names: ['temperature', 'humidity', 'pressure']

An alternative using np.array() directly on the values produces rows instead of columns:

import numpy as np

data = {
'temperature': [72, 75, 68, 71],
'humidity': [45, 50, 55, 48]
}

# Values as rows
arr_rows = np.array(list(data.values()))
print(f"As rows (shape {arr_rows.shape}):")
print(arr_rows)

# Transpose to get values as columns
arr_cols = arr_rows.T
print(f"\nAs columns (shape {arr_cols.shape}):")
print(arr_cols)

Output:

As rows (shape (2, 4)):
[[72 75 68 71]
[45 50 55 48]]

As columns (shape (4, 2)):
[[72 45]
[75 50]
[68 55]
[71 48]]

Handling Nested Dictionaries

When dealing with nested dictionaries, the conversion depends on what structure you need:

import numpy as np

# Nested dictionary representing student grades
students = {
'Alice': {'math': 90, 'science': 85, 'english': 92},
'Bob': {'math': 78, 'science': 88, 'english': 80},
'Charlie': {'math': 95, 'science': 91, 'english': 87}
}

# Extract the numeric values as a 2D array
names = list(students.keys())
subjects = list(students['Alice'].keys())
grades = np.array([list(scores.values()) for scores in students.values()])

print(f"Students: {names}")
print(f"Subjects: {subjects}")
print(f"\nGrades array:")
print(grades)
print(f"\nAverage per student: {grades.mean(axis=1)}")
print(f"Average per subject: {grades.mean(axis=0)}")

Output:

Students: ['Alice', 'Bob', 'Charlie']
Subjects: ['math', 'science', 'english']

Grades array:
[[90 85 92]
[78 88 80]
[95 91 87]]

Average per student: [89. 82. 91.]
Average per subject: [87.66666667 88. 86.33333333]

Common Pitfalls

Mixed Types Without Structured Arrays

When keys and values have different types, NumPy converts everything to strings, making numeric operations impossible:

import numpy as np

data = {'Alice': 90, 'Bob': 85}
arr = np.array(list(data.items()))

print(f"Dtype: {arr.dtype}") # <U21
print(arr[:, 1].sum()) # Tries string concatenation, not addition

Output:

Dtype: <U21
TypeError: the resolved dtypes are not compatible with add.reduce. Resolved (dtype('<U21'), dtype('<U21'), dtype('<U42'))

Solution

Convert only the values to an array, or use structured arrays:

import numpy as np

data = {'Alice': 90, 'Bob': 85}

# Option 1: Values only
values = np.array(list(data.values()))
print(f"Sum: {values.sum()}") # 175

# Option 2: Structured array
structured = np.array(
list(data.items()),
dtype=[('name', 'U10'), ('score', 'i4')]
)

print("Structured array:")
print(structured)

print(f"Sum: {structured['score'].sum()}") # 175

Output:

Sum: 175
Structured array: [('Alice', 90) ('Bob', 85)]
Sum: 175

Quick Comparison of Methods

MethodBest ForPreserves TypesOutput Shape
np.array(list(d.items()))Simple key-value pairs❌ (casts to common type)(n, 2)
np.array(list(d.values()))Numeric values only(n,) or (n, m)
np.column_stack(list(d.values()))Dict of lists → columns(rows, cols)
Structured arrayMixed types (string + numeric)(n,) with named fields

Conclusion

Converting a Python dictionary to a NumPy array can be done in several ways depending on your data and goals:

  • Convert key-value pairs with np.array(list(d.items())) for a simple 2D array of pairs.
  • Convert only values with np.array(list(d.values())) when you need a clean numeric array for computations.
  • Use np.column_stack() when dictionary values are lists that should become columns in a 2D array.
  • Use structured arrays when you need to preserve different data types for keys and values within a single array.

For most data analysis and machine learning tasks, converting only the dictionary's values to a NumPy array is the most practical approach, as it gives you a clean numeric array ready for mathematical operations.