How can I convert Python tuple to C array?


In this article, we will show you how to convert a Python tuple to a C array in python.

Python does not have a built-in array data type like other programming languages, but you can create an array by using a library like Numpy.

Below are the various methods to convert a tuple into an array in Python −

  • Using numpy.asarray() method

  • Use numpy.array() method

If you have not already installed NumPy on your system, run the following command to do so.

pip install numpy

Convert tuple to array using numpy.asarray()

Use the np.asarray() function to convert a Python tuple to an array. The library function np.asarray() converts input to an array. Lists, tuples, tuples, a tuple of tuples, a tuple of lists, and ndarrays are all inputs.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword to import NumPy module with an alias name.

  • Create a variable to store the input tuple.

  • Print the input tuple.

  • Use the type() function(returns the data type of an object) to print the data type of the input tuple.

  • Use the numpy.asarray() function to convert the input tuple to an array by passing the input tuple as an argument to it.

  • Print the output array after converting the input python tuple to an array.

  • Use the type() function(returns the data type of an object) to print the data type of the resultant output array.

Example

The following program converts the input tuple to the array using numpy.asarray() function −

# importing NumPy module with an alias name import numpy as np # input tuple inputTuple = (12, 1, 3, 18, 5) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of InputTuple:", type(inputTuple)) # converting python tuple to an array using numpy.asarray() function outputArray = np.asarray(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple to array:\n", outputArray) # printing the data type of the output array print("Type of OutputArray:", type(outputArray))

Output

On executing, the above program will generate the following output −

InputTuple: (12, 1, 3, 18, 5)
Type of InputTuple: 
Output array after conversion of input tuple to array:
[12 1 3 18 5]
Type of OutputArray: <class 'numpy.ndarray'="">

Convert a tuple of lists into an array using numpy.asarray()

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input tuple of lists.

  • Print the input tuple.

  • Use the type() function(returns the data type of an object) to print the data type of the input tuple.

  • Use the numpy.asarray() function to convert the input tuple of lists to an array by passing the input tuple as an argument to it.

  • Print the output array after converting the input tuple of lists to an array.

  • Use the type() function(returns the data type of an object) to print the data type of the resultant output array.

  • Apply the flatten() function(flattens the ndarray to 1-dimension) on the output array to flatten the output array to 1-dimension.

  • Print the resultant flattened array.

Example

The following program converts the input tuple of lists to the array using numpy.asarray() function and also returns the flattened array of it −

# importing NumPy module with an alias name import numpy as np # input tuple inputTuple = ([1, 10, 5], [3, 6, 4]) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tupl print("Type of Input Tuple:", type(inputTuple)) # converting python tuple of lists to an array using numpy.asarray() function outputArray = np.asarray(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple of lists to array:\n", outputArray) # printing the data type of the output array print("Type of Output Array:", type(outputArray)) # flattening the output array to 1-dimension flatten_array = outputArray.flatten() # printing the flattened array print("Flattened Array:", flatten_array)

Output

On executing, the above program will generate the following output −

InputTuple: ([1, 10, 5], [3, 6, 4])
Type of Input Tuple: <class 'tuple'>
Output array after conversion of input tuple of lists to array:
[[ 1 10 5]
[ 3 6 4]]
Type of Output Array: <class 'numpy.ndarray'>
Flattened Array: [ 1 10 5 3 6 4]

The numpy.asarray() function creates an array from a tuple of lists. Still, it will create a two-dimensional array, which may be flattened using the array.flatten() method.

Convert tuple to array using numpy.array()

The numpy.array() function accepts a Python object and returns an array. We will pass a tuple object to the np.array() function, which will transform it into an array.

Example

The following program converts the input tuple to the array using numpy.array() function −

# importing numpy module with an alias name import numpy as np # input tuple inputTuple = (12, 1, 3, 18, 5) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of InputTuple:", type(inputTuple)) # converting python tuple to an array using numpy.array() function outputArray = np.array(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple to array:\n", outputArray) # printing the data type of the output array print("Type of OutputArray:", type(outputArray))

Output

On executing, the above program will generate the following output −

InputTuple: (12, 1, 3, 18, 5)
Type of InputTuple: <class 'tuple'>
Output array after conversion of input tuple to array:
[12 1 3 18 5]
Type of OutputArray: <class 'numpy.ndarray'>

Convert a tuple of lists into an array using numpy.array()

Example

The following program converts the input tuple of lists to an array using numpy.array() function and also returns the flattened array of it −

# importing numpy module with an alias name import numpy as np # input tuple inputTuple = ([1, 10, 5], [3, 6, 4]) # printing input tuple print("InputTuple:", inputTuple) # printing the data type of the input tuple print("Type of Input Tuple:", type(inputTuple)) # converting python tuple of lists to an array using numpy.array() function outputArray = np.array(inputTuple) # printing output array after conversion print("Output array after conversion of input tuple of lists to array:\n", outputArray) # printing the data type of the output array print("Type of Output Array:", type(outputArray)) # flattening the output array to 1-dimension flatten_array = outputArray.flatten() # printing the flattened array print("Flattened Array:", flatten_array)

Output

On executing, the above program will generate the following output −

InputTuple: ([1, 10, 5], [3, 6, 4])
Type of Input Tuple: <class 'tuple'>
Output array after conversion of input tuple of lists to array:
[[ 1 10 5]
[ 3 6 4]]
Type of Output Array: <class 'numpy.ndarray'>
Flattened Array: [ 1 10 5 3 6 4]

Conclusion

In this post, we learned how to use the Numpy module's array() and asarray() functions to convert python tuples to c arrays. In this post, we also learnt how to convert a list of tuples to an array and flatten them.

Updated on: 09-Nov-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements