How to convert a list and tuple into NumPy arrays?
Last Updated :
23 Aug, 2023
In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python.
Example:
Input: [3, 4, 5, 6]
Output: [3 4 5 6]
Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])
Output: [[8 4 6]
[1 2 3]]
Explanation: Tuple is converted into NumPy Array
Convert a List into NumPy Arrays
These are the methods by which we can convert a list into NumPy Arrays in Python.
Convert a List into NumPy Arrays using numpy.asarray()
numpy.asarray() method converts the input list into NumPy array. In this example, we are converting Python list into NumPy array by using numpy.asarray().
Python3
import numpy as np
# list
list1 = [3, 4, 5, 6]
print(type(list1))
print(list1)
print()
# conversion
array1 = np.asarray(list1)
print(type(array1))
print(array1)
print()
Output:
<class 'list'>
[3, 4, 5, 6]
<class 'numpy.ndarray'>
[3 4 5 6]
Convert a List into NumPy Arrays using numpy.array()
numpy.array() method converts the input list into NumPy array. In this example, we are converting the Python list into NumPy array by using numpy.array().
Python3
import numpy as np
# list
list1 = [1, 2, 3]
print(type(list1))
print(list1)
print()
# conversion
array1 = np.array(list1)
print(type(array1))
print(array1)
print()
Output:
<class 'list'>
[1, 2, 3]
<class 'numpy.ndarray'>
[1 2 3]
Convert a Tuple into NumPy Arrays
These are the methods by which we can convert a tuple into NumPy Arrays.
Convert a Tuple into NumPy Arrays using numpy.asarray()
numpy.asarray() method converts the input tuple into NumPy array. In this example, we are converting the Python tuple into NumPy array by using numpy.asarray().
Python3
# tuple
tuple1 = ([8, 4, 6], [1, 2, 3])
print(type(tuple1))
print(tuple1)
print()
# conversion
array2 = np.asarray(tuple1)
print(type(array2))
print(array2)
Output:
<class 'tuple'>
([8, 4, 6], [1, 2, 3])
<class 'numpy.ndarray'>
[[8 4 6]
[1 2 3]]
Convert a Tuple into NumPy Arrays using numpy.array()
numpy.array() method convert input tuple into NumPy array. In this example, we are converting the Python tuple into NumPy array by using numpy.array().
Python3
# tuple
tuple1 = ([8, 4, 6], [1, 2, 3])
print(type(tuple1))
print(tuple1)
print()
# conversion
array2 = np.array(tuple1)
print(type(array2))
print(array2)
Ouput:
<class 'tuple'>
([8, 4, 6], [1, 2, 3])
<class 'numpy.ndarray'>
[[8 4 6]
[1 2 3]]
Similar Reads
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to convert a dictionary into a NumPy array? In this article, we will learn how to convert a Python Dictionary into a numpy array which is more efficient for numerical operations and provides powerful tools for matrix and array manipulationsKey Steps to Convert a Dictionary to a NumPy ArrayUse dict.items(): This returns key-value pairs from th
3 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array.
2 min read