AppDividend
Latest Code Tutorials

How to Convert Python Tuple to Array

0

Python does not have an inbuilt array data type like other programming languages but if you use a library like Numpy then you will be able to create an array.

To convert a tuple into an array, use one of the following functions.

  1. Using numpy.asarray() method
  2. Using numpy.array() method

Python Tuple to Array

To convert Python tuple to array, use the np.asarray() function. The numpy asarray() function converts the input to an array. This includes lists, list of tuples, tuples, tuple of tuples, tuple of lists, and ndarrays.

If you have not installed numpy previously in your system, then to install numpy in your system, type the following command.

python3 -m pip install numpy

Example

import numpy as np

tup = (11, 21, 19, 18, 46, 29)
print(tup)
print(type(tup))

print("After converting Python tuple to array")

arr = np.asarray(tup)
print(arr)
print(type(arr))

Output

(11, 21, 19, 18, 46, 29)
<class 'tuple'>
After converting Python tuple to array
[11 21 19 18 46 29]
<class 'numpy.ndarray'>

In this example, we have imported numpy module.

Then defined a tuple using the tuple() function. To check the data type in Python, use the type() function.

Using the np.asarray() function and passing a tuple as an argument, we got the array in return. If we check the data type of return value, then it is a numpy array, which is what we wanted.

Convert a tuple of lists into array

To convert a tuple of lists into an array, use the np.asarray() function and then flatten the array using the flatten() method to convert it into a one-dimensional array.

import numpy as np

tup = ([11, 21, 19], [18, 46, 29])

print("After converting Python tuple of lists to array")

arr = np.asarray(tup)
print(arr)

fla_arr = arr.flatten()
print(fla_arr)

Output

After converting Python tuple of lists to array
[[11 21 19]
 [18 46 29]]
[11 21 19 18 46 29]

The numpy.asarray() converts a tuple of lists to array, but it will create a two-dimensional array, and to convert it into a one-dimensional array, use the array.flatten() method.

Using np.array() method to convert tuple to array

The numpy.array() method takes a Python object as an argument and returns an array. We will pass a tuple object to the np.array() function and it will convert that tuple to an array.

import numpy as np

tup = ([11, 21, 19], [18, 46, 29])

print("After converting Python tuple to array using np.array()")

arr = np.array(tup)
print(arr)

print("After flattening the array")
fla_arr = arr.flatten()
print(fla_arr)

Output

After converting Python tuple to array using np.array()
[[11 21 19]
 [18 46 29]]
After flattening the array
[11 21 19 18 46 29]

The np.array() function works almost same as np.asarray() and returns the converted array.

Assuming Python list as an array

If you don’t want to use a numpy array and you want to take the list as an array then use the list comprehension to convert a tuple to an array.

lt = []

tup1 = (11, 19, 21)
tup2 = (46, 18, 29)

lt.append(tup1)
lt.append(tup2)

arr = [x for xs in lt for x in xs]
print(arr)

Output

[11, 19, 21, 46, 18, 29]

That is it for converting Python tuple to array tutorial.

See also

Python tuple to dictionary

Python tuple to list

Python list to tuple

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.