Python Add Elements To An Array - AskPython
Maybe your like
Python doesn’t have a specific data type to represent arrays.
The following can be used to represent arrays in Python:
- By using lists
- By using the array module
- By using the NumPy module
1. Adding to an array using Lists
If we are using List as an array, the following methods can be used to add elements to it:
- By using append() function: It adds elements to the end of the array.
- By using insert() function: It inserts the elements at the given index.
- By using extend() function: It elongates the list by appending elements from both the lists.
Example 1: Adding elements to an array using append() function
my_input = ['Engineering', 'Medical'] my_input.append('Science') print(my_input)Output:
['Engineering', 'Medical', 'Science']
Example 2: Adding elements to an array using extend() function
my_input = ['Engineering', 'Medical'] input1 = [40, 30, 20, 10] my_input.extend(input1) print(my_input)Output:
['Engineering', 'Medical', 40, 30, 20, 10]
Example 3: Adding elements to an array using insert() function
my_input = [1, 2, 3, 4, 5] print(f'Current Numbers List {my_input}') number = int(input("Please enter a number to be added:\n")) index = int(input(f'Enter the index between 0 and {len(my_input) - 1} to add the given number:\n')) my_input.insert(index, number) print(f'Updated List {my_input}')Output:

2. Adding to an array using array module
If we are using the array module, the following methods can be used to add elements to it:
- By using + operator: The resultant array is a combination of elements from both the arrays.
- By using append() function: It adds elements to the end of the array.
- By using insert() function: It inserts the elements at the given index.
- By using extend() function: It elongates the list by appending elements from both the lists.
Example:
import array s1 = array.array('i', [1, 2, 3]) s2 = array.array('i', [4, 5, 6]) print(s1) print(s2) s3 = s1 + s2 print(s3) s1.append(4) print(s1) s1.insert(0, 10) print(s1) s1.extend(s2) print(s1)Output:

3. Addition of elements to NumPy array
We can add elements to a NumPy array using the following methods:
- By using append() function: It adds the elements to the end of the array.
- By using insert() function: It adds elements at the given index in an array.
Example:
import numpy # insert function arr1_insert = numpy.array([1, 23, 33]) arr2_insert = numpy.insert(arr1_insert, 1, 91) print(arr2_insert) # append function arr1_append = numpy.array([4, 2, 1]) arr2_append = numpy.append (arr1_append, [12, 13, 14]) print(arr2_append)Output:
[ 1 91 23 33][ 4 2 1 12 13 14]
Conclusion
Thus, in this article, we have implemented possible ways to add elements to an array.
References
- Python add to an array
- Python array Documentation
- Python NumPy Documentation
Tag » Add Element In Matrix Python
-
How To Add Element To Matrix? - Python - Stack Overflow
-
NumPy: Add Elements In A Matrix, Do Not Add An ... - W3resource
-
Python Add To Array - JournalDev
-
Append/ Add An Element To Numpy Array In Python (3 Ways)
-
How To Add A Number To Each Element Of A Matrix In Python ?
-
sert — NumPy V1.23 Manual
-
Ajouter Une Ligne à Une Matrice Dans NumPy | Delft Stack
-
Python NumPy Array Tutorial - Like Geeks
-
Python Matrix And Introduction To NumPy - Programiz
-
Python For Basic Data Analysis: NP.9 Inserting Elements Into Arrays
-
Numpy.append(): How To Add Elements To A NumPy Array - Codingem
-
How To Append Numpy Array And Insert Elements ?
-
sert() In Python - GeeksforGeeks
-
Python - Matrix - Tutorialspoint
-
Python Arrays - W3Schools
-
Python NumPy Add Tutorial
-
How To ADD Numerical Value To Each Element Of A Matrix Using ...
-
Python Numpy – Add A Constant To All The Elements Of Array
-
How To Add A Column To A NumPy Array In Python - Adam Smith