
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Tuple to Array Conversion Using Python
The Python NumPy library provides various methods to create manipulate and modify arrays in Python Following are the two important methods that helps us to convert a tuple into an array ?
-
Using numpy.asarray() method
Use numpy.array() method
If you haven't already installed NumPy on your system, run the following command to do so.
pip install numpy
Using numpy.asarray() method
This method accepts an objects, as a parameter, and converts it into an array. The given object should be list, list of tuples, tuple, tuple of tuples, tuple of lists and 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))
On executing the above program, it 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'="">
Example
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.
The program converts an input tuple of lists into a NumPy array using the numpy.asarray() function. This then prints the types of both input and output, and flattens the array to one dimension using the flatten() method for display.
# 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)
The result is obtained as follows ?
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]
Tuple to Array Using numpy.array()
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 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))
We will get the output as follows ?
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
This program converts an input tuple of lists into a NumPy array using the numpy.array() function. This 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)
The result is obtained as follows ?
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]