dictionary conversion
python programming
data structures
coding tutorial
array manipulation

Convert dictionary values into array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, converting dictionary values into an array-like structure usually means turning the dictionary’s values view into a list. The standard answer is list(my_dict.values()), but in real code you also need to think about ordering, filtering, type conversion, and whether the values are simple scalars or nested structures.

The basic conversion

Python dictionaries expose a dynamic values view through .values(). Convert it to a list when you need indexing, serialization, or a stable snapshot.

python
1scores = {
2    "alice": 91,
3    "bob": 88,
4    "carol": 95,
5}
6
7values_array = list(scores.values())
8print(values_array)

This produces:

python
[91, 88, 95]

Ordering behavior

In modern Python, dictionary iteration preserves insertion order. That means the list returned from list(d.values()) follows the order in which keys were inserted.

That is often exactly what you want, but it is still worth being explicit if the consumer of the result depends on a particular order.

If you need sorted output, sort by keys or values intentionally.

python
prices = {"c": 12, "a": 5, "b": 9}
ordered_values = [prices[key] for key in sorted(prices)]
print(ordered_values)

Filtering while converting

A list comprehension is the cleanest way to filter values during conversion.

python
1inventory = {
2    "pencil": 120,
3    "pen": 0,
4    "eraser": 35,
5    "marker": 0,
6}
7
8available_counts = [count for count in inventory.values() if count > 0]
9print(available_counts)

This is better than converting everything first and filtering afterward unless you specifically need both representations.

Converting value types

Sometimes dictionary values are strings that should become numeric types.

python
prices = {"a": "10.5", "b": "7.25", "c": "12.00"}
price_numbers = [float(v) for v in prices.values()]
print(price_numbers)

This is a good place to normalize data while you are already traversing the values.

Nested dictionary values

If each value is itself a dictionary, decide what you want the array to contain.

python
1users = {
2    1: {"name": "Ana", "age": 30},
3    2: {"name": "Ben", "age": 27},
4    3: {"name": "Chao", "age": 33},
5}
6
7names = [user["name"] for user in users.values()]
8print(names)

If you actually want the nested dictionaries themselves, list(users.values()) is fine. If you want one field from each nested object, extract it explicitly.

When you really need NumPy arrays

Sometimes “array” means a NumPy array rather than a Python list.

python
1import numpy as np
2
3scores = {"alice": 91, "bob": 88, "carol": 95}
4arr = np.array(list(scores.values()))
5print(arr)

That makes sense when you are moving into numeric computation. If you only need a general container, a list is usually simpler.

Snapshot versus live view

One small but important distinction: dict.values() returns a live view, not a detached array. If the dictionary changes later, the view reflects those changes. Converting with list(...) creates a separate snapshot at that moment.

That distinction matters when you pass values to other code and expect them to stay stable even if the original dictionary continues to change afterward.

That is another reason people convert explicitly when passing data to other code.

Common Pitfalls

A common mistake is assuming .values() itself returns a list. It returns a view object.

Another mistake is relying on a specific order without documenting that the order comes from dictionary insertion order.

A third mistake is forgetting to normalize nested or string-valued data before treating the result like a numeric array.

Summary

  • Use list(my_dict.values()) for the standard conversion.
  • The resulting order follows dictionary insertion order in modern Python.
  • Use comprehensions to filter or transform values during conversion.
  • Extract nested fields explicitly when values are dictionaries.
  • Convert to NumPy only when you actually need numeric array behavior.
  • Convert with list(...) when you want a detached snapshot rather than a live view.

Course illustration
Course illustration

All Rights Reserved.