Extend python array using extend() method

Submitted 3 years, 6 months ago
Ticket #188
Views 214
Language/Framework Python
Priority Low
Status Closed


 I tried this code extend method , some how my code is not working

my_array = array('i', [1,2,3,4,5]) 
my_extnd_array = array('i', [7,8,9,10]) 
my_array.extend(my_extnd_array)

i was getting error

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    my_extnd_array = array('i', [7,8,9,10])
NameError: name 'array' is not defined

Submitted on Oct 03, 20
add a comment

1 Answer

Verified

Please refer to the error It's saying NameError: name `array` is not defined and it seems you have not imported the array module which is required for array creation.

You can try the given code.

import array

my_array = array.array('i', [1,2,3,4,5])
my_extnd_array = array.array('i', [6,7,8,9,10])

my_array.extend(my_extnd_array)

print(my_array) # array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

"""
You can also use the below-mentioned import. It will allow to directly create an array without specifying the array module


# from array import array

my_array = array('i', [1,2,3,4,5])
my_extnd_array = array('i', [6,7,8,9,10])

my_array.extend(my_extnd_array)

print(my_array) # array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

"""

Submitted 3 years, 6 months ago


Latest Blogs