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
Hey, folks! In this article, we will focus on ways to append an array in Python. In programming terms, an array is a linear data structure that stores similar kinds of elements.What is Python Array?
As we all know, Python does not offer us with a specific data type — ‘array’. Rather, the following variants of Python Array are available for us to use–
- Python List: It contains all the functionalities of an Array.
- Python Array module: This module is used to create an array and manipulate the data with the specified functions.
- Python NumPy array: The NumPy module creates an array and is used for mathematical purposes.
Now, let us understand the ways to append elements to the above variants of Python Array.
Append an Array in Python Using the append[] function
Python append[] function
enables us to add an element or an array to the end of another array. That is, the specified element gets appended to the end of
the input array.
The append[] function has a different structure according to the variants of Python array mentioned above.
Let us now understand the functioning of Python append[] method on each variant of Python Array.
Variant 1: Python append[] function with Lists
Lists are considered as dynamic arrays. Python append[] method can be framed here to add/append elements to the end of the list.
Syntax:
list.append[element or list]
The list or the element gets added to the end of the list and the list is updated with the added element.
Example:
lst = [10,20,30,40] x = [0,1,2] lst.append[x] print[lst]
Output:
[10, 20, 30, 40, [0, 1, 2]]
Variant 2: Python append[] method with the Array module
We can create an array using the Array module and then apply the append[] function to add elements to it.
Initialize a Python array using the array module:
import array array.array['unicode',elements]
unicode
: It represents the type of elements to be occupied by the array. For example, ‘d’ represents double/float elements.
Further, the append[] function operates in the same manner as that with Python Lists.
Example:
import array x = array.array['d', [1.4, 3.4]] y = 10 x.append[y] print[x]
Output:
array['d', [1.4, 3.4, 10.0]]
Variant 3: Python append[] method with NumPy array
The NumPy module can be used to create an array and manipulate the data against various mathematical functions.
Syntax: Python numpy.append[] function
numpy.append[array,value,axis]
array
: It is the numpy array to which the data is to be appended.value
: The data to be added to the array.axis
[Optional]: It specifies row-wise or column-wise operations.
In the below example, we have used numpy.arange[] method to create an array within the specified range of values.
Example:
import numpy as np x = np.arange[3] print["Array x : ", x] y = np.arange[10,15] print["\nArray y : ", y] res = np.append[x, y] print["\nResult after appending x and y: ", res]
Output:
Array x : [0 1 2] Array y : [10 11 12 13 14] Result after appending x and y: [ 0 1 2 10 11 12 13 14]
Conclusion
That’s all for this topic. Feel free to comment below, in case you come across any doubt. For more such posts related to Python, do visit [email protected].
References
- Python add to array — JournalDev
- NumPy append[] method — JournalDev