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 module
import array as arr
# creating an integer data type array
x = arr.array('i', [1,2,3,4,5,6,7])
# creatomg a list
y = [8, 9, 10]
# using the fromlist() function
x.fromlist(y)
# Printing the new array
print(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 in y to the array x.
  • Line 15: We print the new array x which now has the list values in y added to its ending.

Free Resources