w3resource logo
Python Exercises

Python NumPy: Convert a list and tuple into arrays

Python NumPy: Array Object Exercise-11 with Solution

Write a Python program to convert a list and tuple into arrays.

Sample Solution:-

Python Code:

import numpy as np
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))

Sample Output:

List to array:                                                          
[1 2 3 4 5 6 7 8]                                                       
Tuple to array:                                                         
[[8 4 6]                                                                
 [1 2 3]]

Python Code Editor:

import numpy as np
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))

Improve this sample solution and post your code through Disqus