Python | Convert Numpy Arrays to Tuples
Last Updated :
06 Apr, 2023
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 and map
Step-by-step approach :
- Import the NumPy library with the alias np.
- Initialize a 2D NumPy array named ini_array with two rows and two columns.
- Convert the NumPy array into a tuple of tuples using the map() function and tuple() constructor. This is done by applying the tuple() function to each row of the NumPy array using map().
- Assign the resulting tuple of tuples to a variable called result.
- Print the result array as a string using the print() function.
Python3
# Python code to demonstrate
# deletion of columns from numpy array
import numpy as np
# initialising numpy array
ini_array = np.array([['manjeet', 'akshat'], ['nikhil', 'akash']])
# convert numpy arrays into tuples
result = tuple(map(tuple, ini_array))
# print result
print ("Resultant Array :"+str(result))
Output:
Result:(('manjeet', 'akshat'), ('nikhil', 'akash'))
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.
Method #2: Using Naive Approach
Python3
# Python code to demonstrate
# deletion of columns from numpy array
import numpy as np
# initialising numpy array
ini_array = np.array([['manjeet', 'akshat'], ['nikhil', 'akash']])
# convert numpy arrays into tuples
result = tuple([tuple(row) for row in ini_array])
# print result
print ("Result:"+str(result))
Output:
Result:(('manjeet', 'akshat'), ('nikhil', 'akash'))
Similar Reads
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
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 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 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array.
2 min read
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 NumPy array to dictionary in Python? 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 givi
3 min read