Python append to array – In Python, How do you append an Array?

Python Array:

Python append to array: An array in Python is a linear data structure that stores similar data items in contiguous memory locations.

When compared to a List which is a dynamic Array, Python Arrays keeps elements of the same datatype. A Python List, on the other hand, can store entries of several/different data types.

Python, as we all know, does not provide us with a specific data type -‘array’. Rather, we have the following Python Array variants to choose from (an array can be appended as follows):

  • List of Python: It has all of the characteristics of an Array.
  • Array module in Python: This module is used to construct an array and alter the data using the functions provided.
  • Python NumPy array: The NumPy module generates an array for mathematical applications.

Using append Function, to append an Array

Python append array: The append() method in Python allows us to attach/add an element or array to the end of another array. To put it another way, the given element is appended to the end of the input array.

The append() function has a distinct structure depending on the Python array variants discussed above.

Here we look at how the Python append() technique works on each variant of the Python Array.

1)append() Function in Python with lists

Array append python: Lists are treated as dynamic arrays. The Python append() method can be used to add or append elements to the end of the list.

Syntax:

list.append(element/list)

Example

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Append or add the given second list to the first list using the append() function.
  • Print the given first list after appending the second list to it.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
gvn_lst1 = [15, 16, 17, 18, 19]
# Give the second list as static input and store it in another variable.
gvn_lst2 = [21, 22, 23]
# Append or add the given second list to the first list using the append() function
gvn_lst1.append(gvn_lst2)
# Print the given first list after appending the second list to it.
print("The given first list after appending the second list to it = \n", gvn_lst1)

Output:

The given first list after appending the second list to it = 
 [15, 16, 17, 18, 19, [21, 22, 23]]

2)append() Function in Python with Array Module

Append to array python: Using the Array module, we can create an array and then append elements to it using the append() function.

How to Initialize an array in Python using the array module?

import array
array.array('unicode', elements)

unicode: The unicode represents the type of elements that will be occupied by the array. For example, the letter ‘d’ stands for double/float elements.
Moreover, the add() function works in the same way as Python Lists do.

Approach:

  • Import array module using the import Keyword.
  • Give the array as static input using the array function by passing the unicode, some random elements as arguments to it.
  • Store it in a variable (here ‘d’ represents double or floating elements).
  • Give the random number as static input and store it in another variable.
  • Append the above-given number to the given array using the append() function.
  • Print the given array after appending the above-given number.
  • The Exit of the Program.

Below is the implementation:

# Import array module using the import Keyword
import array
# Give the array as static input using the array function by passing the unicode,
# some random elements as arguments to it.
# Store it in a variable.
# here 'd' represents double or float elements.
gvn_arry = array.array('d', [1, 8.2, 4.6, 7])
# Give the random number as static input and store it in another variable.
gvn_num = 50
# Append the above given number to the given array using the append() function.
gvn_arry.append(gvn_num)
# Print the given array after appending the  above given number.
print("The given array after appending the  above given number:\n", gvn_arry)

Output:

The given array after appending the above given number: 
array('d', [1.0, 8.2, 4.6, 7.0, 50.0])

3)append() Function in Python with Numpy Array

Append array python: The NumPy module is used to construct an array and then alter the data using various mathematical functions.

Syntax:

numpy.append(array, value, axis)

Parameters

array: It is the data is to be inserted into the numpy array.

value: It is the data to be added to the array.

axis: It provides row or column-wise operations.

Example

Approach:

  • Import numpy module as np using the import Keyword.
  • Create a first array using the arange() function by passing some random number to it.
  • Store it in a variable.
  • The numpy.arange() method is used to construct an array with values within the given range.
  • Similarly, create another array and Store it in another variable.
  • Add both the arrays using the append() function(second array with the first).
  • Store it in another variable.
  • Print the above obtained array after appending first and the second array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module as np using the import Keyword
import numpy as np 
# Create a first array using the arange() function by passing some random number to it
# The numpy.arange() method is used to construct an array with values within the given range. 
# Store it in a variable.
gvn_arry1= np.arange(4) 
print("The given first array =  ", gvn_arry1) 
# similarly create another array and Store it in another variable. 
gvn_arry2 = np.arange(20,25) 
print("The given second array = ", gvn_arry2) 
# Add both the arrays using the append() function(second array with the first)
# Store it in another variable. 
rslt_arry = np.append(gvn_arry1, gvn_arry2)
print("The obtained array after appending first and the second array:\n ", rslt_arry)

Output:

The given first array = [0 1 2 3] 
The given second array = [20 21 22 23 24] 
The obtained array after appending first and the second array: 
[ 0 1 2 3 20 21 22 23 24]