Python doesn’t have a specific data type to represent arrays. The following can be used to represent arrays in Python: If we are using List as an array, the following methods can be used to add elements to it: Example 1: Adding elements to an array using append[] function 1. Adding to an array using Lists
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.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
Python doesn’t have any specific data type as an array. We can use List that has all the characteristics of an array. Python array module can be used to create an array of integers and floating-point numbers. If you want to do some mathematical operations on an array, you should use the NumPy module.
1. Python add to Array
- If you are using List as an array, you can use its append[], insert[], and extend[] functions. You can read more about it at Python add to List.
- If you are using array module, you can use the concatenation using the + operator, append[], insert[], and extend[] functions to add elements to the array.
- If you are using NumPy arrays, use the append[] and insert[] function.
2. Adding elements to an Array using array module
- Using + operator: a new array is returned with the elements from both the arrays.
- append[]: adds the element to the end of the array.
- insert[]: inserts the element before the given index of the array.
- extend[]: used to append the given array elements to this array.
import array
arr1 = array.array['i', [1, 2, 3]]
arr2 = array.array['i', [4, 5, 6]]
print[arr1] # array['i', [1, 2, 3]]
print[arr2] # array['i', [4, 5, 6]]
arr3 = arr1 + arr2
print[arr3] # array['i', [1, 2, 3, 4, 5, 6]]
arr1.append[4]
print[arr1] # array['i', [1, 2, 3, 4]]
arr1.insert[0, 10]
print[arr1] # array['i', [10, 1, 2, 3, 4]]
arr1.extend[arr2]
print[arr1] # array['i', [10, 1, 2, 3, 4, 4, 5, 6]]
3. Adding elements to the NumPy Array
- append[]: the given values are added to the end of the array. If the axis is not provided, then the arrays are flattened before appending.
- insert[]: used to insert values at the given index. We can insert elements based on the axis, otherwise, the elements will be flattened before the insert operation.
>>> import numpy as np
>>> np_arr1 = np.array[[[1, 2], [3, 4]]]
>>> np_arr2 = np.array[[[10, 20], [30, 40]]]
>>>
>>> np.append[np_arr1, np_arr2]
array[[ 1, 2, 3, 4, 10, 20, 30, 40]]
>>>
>>> np.append[np_arr1, np_arr2, axis=0]
array[[[ 1, 2],
[ 3, 4],
[10, 20],
[30, 40]]]
>>>
>>> np.append[np_arr1, np_arr2, axis=1]
array[[[ 1, 2, 10, 20],
[ 3, 4, 30, 40]]]
>>>
>>> np.insert[np_arr1, 1, np_arr2, axis=0]
array[[[ 1, 2],
[10, 20],
[30, 40],
[ 3, 4]]]
>>>
>>> np.insert[np_arr1, 1, np_arr2, axis=1]
array[[[ 1, 10, 30, 2],
[ 3, 20, 40, 4]]]
>>>
4. References
- array module
- numpy.append[] docs