- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to convert a NumPy array to a dictionary in Python?

This tutorial provides a step-by-step guide on how to convert a NumPy array to a dictionary using Python. In NumPy, an array is essentially a table of elements that are typically numbers and share the same data type. It is indexed by a tuple of positive integers, and the number of dimensions of the array is referred to as its rank. The size of the array along each dimension is defined by a tuple of integers known as the shape of the array. The NumPy array class is known as **ndarray**, and its elements can be accessed by using square brackets. Initializing NumPy arrays can be achieved using nested Python lists.

Converting a NumPy array to a dictionary in Python can be useful when you need to perform certain operations on the array, such as sorting or searching, and require a dictionary as an input. The resulting dictionary will have keys corresponding to the indices of the array elements and values corresponding to the array elements themselves.

With the help of the step-by-step instructions provided in this tutorial, you'll be able to easily convert your NumPy array to a dictionary in Python.

The approach to converting a NumPy array to a dictionary involves three main steps −

The first step is to use the flatten function to create a copy of the array that is collapsed into a single dimension. This is useful when we want to access each element of the array sequentially.

The second step is to use the enumerate function, which automatically generates a counter or index for each item in the list. The counter or index starts from 0 by default, but we can specify a different starting value if needed. In this case, the counter or index corresponds to the key of the dictionary.

Finally, we use the dict function to convert the flattened array with its enumerated indices into a dictionary. The resulting dictionary will have keys corresponding to the indices of the flattened array and values corresponding to the elements of the array.

Now let's focus on the code example where we will convert a NumPy array to a dictionary in Python.

## Example 1: Converting numPy array to a dictionary

This code creates a 3×3 NumPy array of integers and then uses the dict and enumerate functions to convert it to a dictionary. The resulting dictionary has keys corresponding to the indices of the flattened array and values corresponding to the elements of the array. We then print both the original array and the resulting dictionary to verify the conversion.

Consider the code shown below.

# importing required libraries import numpy as np # creating a numpy array array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # convert numpy array to dictionary d = dict(enumerate(array.flatten(), 1)) # print numpy array print(array) print(type(array)) # print dictionary print(d) print(type(d))

To run the above code, we need to run the command shown below.

python3 main.py

### Output

Once we run the above command in the terminal, we will get the following **output**

[[1 2 3] [4 5 6] [7 8 9]] <class 'numpy.ndarray'> {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} <class 'dict'>

### Example 2

In this version of the code, we use the enumerate function to create key-value pairs for each row of the NumPy array. The resulting dictionary has keys 1, 2, and 3 (corresponding to the row index plus 1) and values [1, 2, 3], [4, 5, 6], and [7, 8, 9] respectively.

We then print both the original NumPy array and the resulting dictionary to verify the conversion.

Consider the code shown below.

# importing required libraries import numpy as np # creating a numpy array array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # convert numpy array to dictionary d = dict(enumerate(array, 1)) # print numpy array print(array) print(type(array)) # print dictionary print(d) print(type(d))

### Output

[[1 2 3] [4 5 6] [7 8 9]] <class 'numpy.ndarray'> {1: array([1, 2, 3]), 2: array([4, 5, 6]), 3: array([7, 8, 9])} <class 'dict'>

## Conclusion

In conclusion, converting a NumPy array to a dictionary in Python can be done easily using various methods. The simplest approach is to use the dict() function in combination with the enumerate() function and the flatten() method of the NumPy array.

Another approach is to use a dictionary comprehension to iterate over the rows of the array and create key-value pairs based on the row index and the row itself. These methods allow us to represent a NumPy array as a dictionary, which can be useful for various applications that require a dictionary data structure.

With the help of the examples provided in this tutorial, you should be able to easily convert NumPy arrays to dictionaries in your Python code.