How to convert NumPy array to dictionary in Python? Last Updated : 26 Feb, 2023 Comments Improve Suggest changes Like Article Like Report The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists. Approach To convert a numpy array to dictionary the following program uses dict(enumerate(array.flatten(), 1)) and this is what it exactly does: array.flatten: This function is used to get a copy of given array, collapsed into one dimension.enumerate: Enumerate method comes with an automatic counter/index for each of the items present in the list. The first index value will start from 0dict: this function is used to convert any object to dictionary. Example 1: Python3 # importing required libraries import numpy as np # creating a numpy array array = np.array([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]) # 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)) Output: [['a' 'b' 'c'] ['d' 'e' 'f'] ['g' 'h' 'i']] <class 'numpy.ndarray'> {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i'} <class 'dict'> Time Complexity: The time complexity for converting a numpy array to a dictionary is O(n), where n is the number of elements in the numpy array. Space Complexity: The space complexity for converting a numpy array to a dictionary is O(n), where n is the number of elements in the numpy array. Example 2: Python3 # importing required libraries import numpy as np # creating a numpy array array = np.array([['1', '2', '3','4','5'], ['6', '7', '8','9','10'], ['11', '12', '13','14','15']]) # 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)) Output: [['1' '2' '3' '4' '5'] ['6' '7' '8' '9' '10'] ['11' '12' '13' '14' '15']] <class 'numpy.ndarray'> {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', 11: '11', 12: '12', 13: '13', 14: '14', 15: '15'} <class 'dict'> Time complexity: O(n), where n is the number of elements in the numpy array. Auxiliary space: O(n), where n is the number of elements in the numpy array, due to the creation of the dictionary. Comment More infoAdvertise with us Next Article How to convert NumPy array to dictionary in Python? mukulsomukesh Follow Improve Article Tags : Python Python-Quizzes Python-numpy Practice Tags : python Similar Reads Ways to Convert a Python Dictionary to a NumPy Array The task of converting a dictionary to a NumPy array involves transforming the dictionaryâs key-value pairs into a format suitable for NumPy. In Python, there are different ways to achieve this conversion, depending on the structure and organization of the resulting array.For example, consider a dic 3 min read How to convert a dictionary into a NumPy array? It's sometimes required to convert a dictionary in Python into a NumPy array and Python provides an efficient method to perform this operation. Converting a dictionary to NumPy array results in an array holding the key-value pairs in the dictionary. Python provides numpy.array() method to convert a 3 min read Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into 4 min read How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us 4 min read How To Convert Numpy Array To Tensor? We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho 2 min read How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can 3 min read How to Convert images to NumPy array? Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. Â In this article we will see How to Convert images to NumPy array? Mod 6 min read Python | Convert Numpy Arrays to Tuples Given a numpy array, write a program to convert numpy array into tuples. Examples - Input: ([[1, 0, 0, 1, 0], [1, 2, 0, 0, 1]]) Output: ((1, 0, 0, 1, 0), (1, 2, 0, 0, 1)) Input: ([['manjeet', 'akshat'], ['nikhil', 'akash']]) Output: (('manjeet', 'akshat'), ('nikhil', 'akash')) Method #1: Using tuple 2 min read Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p 3 min read NumPy ndarray.tolist() Method | Convert NumPy Array to List The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars. Data items are converted to the nearest compatible built-in Python type. Example Python3 import numpy as np gfg = np.array([1, 2, 3, 4, 5]) print 1 min read Like