Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Python does not have a built-in array data type like other programming languages, but you can create an array using a library like NumPy.
Installation
If you haven't already installed NumPy on your system, run the following command to do so ?
pip install numpy
Methods for Tuple to Array Conversion
The Python NumPy library provides various methods to create, manipulate and modify arrays in Python. Following are the two important methods that help us convert a tuple into an array ?
Using numpy.asarray() method
Using numpy.array() method
Using numpy.asarray() Method
This method accepts an object as a parameter and converts it into an array. The given object should be a list, list of tuples, tuple, tuple of tuples, tuple of lists, or ndarrays (i.e., an object that could be converted into an array).
Example
The following program converts an input tuple to an array using the numpy.asarray() function. It displays the input tuple, its type, the converted array, and the array's type for verification ?
# 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))
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'>
Example with Nested Tuples
The numpy.asarray() function creates an array from a tuple of lists. It will create a two-dimensional array, which may be flattened using the array.flatten() method ?
# 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.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)
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]
Using numpy.array() Method
The numpy.array() function accepts any Python object and returns an array. We need to pass a tuple object to the np.array() function to convert it into an array.
Example
This program converts an input tuple into a NumPy array using the numpy.array() function and displays the types before and after the conversion ?
# 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))
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'>
Example with Nested Tuples
This program converts an input tuple of lists into a NumPy array using the numpy.array() function. It then prints their types and returns the array in one dimension ?
# 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)
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]
Comparison
| Method | Performance | Memory Usage | Best For |
|---|---|---|---|
numpy.asarray() |
Faster (no copy if possible) | Lower | When input might already be an array |
numpy.array() |
Slower (always creates copy) | Higher | When you need a new array copy |
Conclusion
Both numpy.asarray() and numpy.array() effectively convert Python tuples to NumPy arrays. Use asarray() for better performance when memory efficiency is important, and array() when you need to ensure a new copy is created.
