What does the array extend() function do in Python?
Overview
An array in python is used to store multiple values or elements of the same datatype in a single variable.
The extend() function is simply used to attach an item from iterable to the end of the array. In a simpler term, this method is used to add an array of values to the end of a given or existing array.
Syntax
array.extend(iterable)
Parameter value
The extend() function takes an array as its parameter value.
Example
# importing the array moduleimport array as arr# creating an integer data type arrayx = arr.array('i', [1,2,3,4,5,6,7])# using the extend function to add an array to the existing arrayx.extend([8,9,10])# printing the new arrayprint(x)
Explanation
- Line 2: We import the array module.
- Line 5: We use the
array.array()function to create an integer type of array. - Line 8: We use the
extend()method to add an array with elements8,9, and10to the original array. - Line 11: We print the new extended array.