Python Array tolist() Method
The Python array tolist() method is used to convert an array to a list with same elements or items.
Syntax
Following is the syntax of the Python array tolist() method β
array_name.tofile()
Parameters
This method does not accepts any parameter.
Return Value
This method returns a list that contains same items of the array.
Example 1
Following is the basic example of the Python array tolist() method β
import array as arr
myArray = arr.array("i",[4,5,6,7])
print("Array Elements :",myArray)
myList = myArray.tolist()
print("Elements After Conversion: ", myList)
Output
Following is the output of the above code β
Array Elements : array('i', [4, 5, 6, 7])
Elements After Conversion : [4, 5, 6, 7]
Example 2
Lets try convert an array of double datatype into list β
import array as arr
myArray = arr.array("d",[56.7,97.8,24.6,47.9])
print("Array Elements :",myArray)
list = myArray.tolist()
print("Elements After Conversion :", list)
Output
Array Elements : array('d', [56.7, 97.8, 24.6, 47.9])
Elements After Conversion : [56.7, 97.8, 24.6, 47.9]
Example 3
Lets try to convert array into list without tolist() method.
Here, We have created an array of int datatype, and empty list is created and trying to append all the elements into it β
import array as arr
array = arr.array("i",[10,20,30,40,50,60])
print("Array Elements :",array)
list = []
for i in array:
list.append(i)
print("Elements After Conversion :", list)
Output
Following is the output of the above code β
Array Elements : array('i', [10, 20, 30, 40, 50, 60])
Elements After Conversion : [10, 20, 30, 40, 50, 60]
python_array_methods.htm
Advertisements