Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What does the array extend() function do in Python?

Onyejiaku Theophilus Chidalu

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 module
import array as arr

# creating an integer data type array
x = arr.array('i', [1,2,3,4,5,6,7])

# using the extend function to add an array to the existing array
x.extend([8,9,10])

# printing the new array
print(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 elements 8, 9, and 10 to the original array.
  • Line 11: We print the new extended array.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring