How to add elements to array in Python

List

In this example, we are going to learn How to add elements to array in Python with examples. Python does not provide any specific data type as an array. but there are many alternative ways to append an array in Python that we are going to learn in this post.

How to add elements to array in Python


  • List.append() to add elements to array in Python
  • Array module append() method to add elements to array in Python
  • Numpy Module to add elements to array in Python

1.List.append() to append array in Python


The list is also known as a dynamic array in Python. The list. append() method append/add element or iterable( list, tuple, string, dictionary, set) at end of the list. It appends the whole list object at end of the list, not a single element separated by a comma(‘).

  • Extend(): List.extend() is used to append multiple elements in List.The extend() method is used to combine two lists, add all elements of a list to the end of another list separated by comma(,) whereas list.append() append whole list object at end of the list, not a single element separated by a comma(‘).
  • insert(): List. insert() used to add/append an element at a specific index.

list.append() | Python Program to append to Array using list

mylist = ['C#','lang','Go','Data']

#appending an element

mylist.append(12)

print('list after append an element = \n',mylist)


lstnum = ['16','17','35','68']

#appending lstnum object

mylist.append(lstnum)

print('\n list after append an list = \n',mylist)



Output

list after append an element = 
 ['C#', 'lang', 'Go', 'Data', 12]

list after append an list= 
 ['C#', 'lang', 'Go', 'Data', 12, ['16', '17', '35', '68']]

list.extend() : Python Program to all elements into array in pyton

mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']

#merging lstnum object in mylist

mylist.extend(lstnum)
print(mylist)

Output

['C#', 'lang', 'Go', 'Data', '16', '17', '35', '68']

list.insert() -Python Program to add element into Array at specifc index

mylist = ['C#','lang','Go','Data']
lstnum = ['16','17','35','68']


#append/add element at index 
mylist.insert(0,12)
print('add an element :',mylist)

#append lstnum object in mylist

mylist.insert(1,lstnum)
print('\n add an array :',mylist)

Output

add an element : [12, 'C#', 'lang', 'Go', 'Data']

 add an array : [12, ['16', '17', '35', '68'], 'C#', 'lang', 'Go', 'Data']

2. Array module to add elements in array in Python


The array module append method can also be used to append array in Python to use this module we have to first import it into our program by using “import array”

  • append(): add/append an element ate end of array
  • extend() : add/append an array elements at end of array.It add multiple elements at once
  • insert() :Add element in array at specific index

Syntax

array.array(unicode,elements)

Parameters

  • Unicode: It represents the type of elements that the array contains the ‘i’ is signed integer.
    • ‘d’: It is for double/float
    • ‘i’: it is for signed integer
    • ‘b’ : It is for signed c har

Python Program to add elements to Array Using array module

import array 
numarr = [16,17,35,68]
myarr = array.array('i', [12,14,700,60,50])

element = 10
myarr.append(element)
print(myarr)

#extend an array
myarr.extend(numarr)
print(myarr)

#append element in array at specific index 
myarr.insert(2,6)
print(myarr)

Output

array('i', [12, 14, 700, 60, 50, 10])

array('i', [12, 14, 700, 60, 50, 10, 16, 17, 35, 68])
array('i', [12, 14, 6, 700, 60, 50, 10, 16, 17, 35, 68])

3. NumPy module to add elements to array in Python


NumPy module append() method append an element or NumPy array at the end of the array. This method does not modify the original array else return a copy of the original array after adding the passed element or array.

Numpy module insert() method append an element or NumPy array at a specific index

Python Program to append in Numpy Array

import numpy as np


myarr = np.array([12,14,700,60,50])
numarr = np.array([3,6,9])
indexarr = [2,7]

#add/append an element at end of array

resarr = np.append(myarr, 90)
print('append an element in array: ', resarr)

#add/append array at end of array

resarr = np.append(myarr,numarr)
print('\n appended an Array: ', resarr)


#add/append array at specific index
resarr = np.append(myarr,indexarr,axis =0)

print('\n appended array at specif index: ', resarr)


Output

append an element in array:  [ 12  14 700  60  50  90]

appended an Array:  [ 12  14 700  60  50   3   6   9]

 appended array at specif index:  [ 12  14 700  60  50   2   7]

Summary

In this post we have learned How to add elements to array in Python with Python program examples