How to Convert Tuples to Arrays in Python?

Estimated read time 1 min read

To convert tuples to arrays in Python, you can use the NumPy library. NumPy provides a function called array() that allows you to convert various data types, including tuples, to arrays.

Here’s an example of how to convert a tuple to an array using NumPy:

import numpy as np

tuple_data = (1, 2, 3, 4, 5)  # Example tuple
array_data = np.array(tuple_data)  # Convert tuple to array


In this example, we import the NumPy library using the import numpy as np statement. Then, we define an example tuple named tuple_data containing some values.

Next, we use the np.array() function and pass the tuple_data as an argument. This function converts the tuple to a NumPy array and assigns it to the variable array_data.

Finally, we print the array_data, which will display the converted array:

[1 2 3 4 5]

Note that the resulting array will have the same data type as the elements in the tuple. In this case, since the tuple contains integers, the resulting array will also have the integer data type.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply