How to Convert a Dictionary to a NumPy Array in Python

To convert a dictionary to a NumPy array, use the Python items() function on the dictionary, then convert it to an a list using the Python list() function.


Then create a new NumPy array from this data, pass it as the first argument of np.array().


Here is a full example:


dict = {"a": 1, "b": 2, "c": 3, "d": 4}

data = list(dict.items())
an_array = np.array(data)

[['a' '1']
['b' '2']
['c' '3']
['d' '4']]