What is the array fromlist() function Python?
Overview
An array is used to store multiple items, elements, or values of the same datatype in a single variable.
The fromlist() function is simply used to append or add a list to the ending of a given array.
Syntax
array.fromlist(list)
Parameter value
The fromlist() method takes a list as the parameter value.
Return value
The fromlist() method returns an array having a list added to the end to the array.
Example
# importing the array moduleimport array as arr# creating an integer data type arrayx = arr.array('i', [1,2,3,4,5,6,7])# creatomg a listy = [8, 9, 10]# using the fromlist() functionx.fromlist(y)# Printing the new arrayprint(x)
Explanation
- Line 2: We import the array module in python.
- Line 5: We create an array variable
x. - Line 9: We create a string variable
y. - Line 12: We use the
fromlist()function to add the list inyto the arrayx. - Line 15: We print the new array
xwhich now has the list values inyadded to its ending.