- How to add elements to array in Python
- How to add elements to array in Python
- 1.List.append() to append array in Python
- list.insert() -Python Program to add element into Array at specifc index
- 2. Array module to add elements in array in Python
- Syntax
- Parameters
- Python Program to add elements to Array Using array module
- 3. NumPy module to add elements to array in Python
- Python Program to append in Numpy Array
- Summary
- Add Elements to Python Array
- How to Add Elements to a Python Array?
- Method 1: Adding to an Array in Python by Using Lists
- Method 2: Adding to an Array in Python by Using Array Module
- Method 3: Adding to NumPy Array
- Python add elements to an Array
- Upskill 2x faster with Educative
- 1. Adding to an array using Lists
- 2. Adding to an array using array module
- 3. Addition of elements to NumPy array
- Conclusion
- References
How to add elements to array in Python
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() methodto 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(‘).
['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)
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)
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)
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
Add Elements to Python Array
Adding elements is a basic operation with Python arrays. There are multiple ways to define arrays in Python, such as with lists (an array-like data structure), the array module, or the NumPy library. Each method has different ways to add new elements.
Depending on the array type, there are different methods to insert elements.
This article shows how to add elements to Python arrays (lists, arrays, and NumPy arrays).
- Python 3 installed.
- An IDE or text editor to write and edit code.
- An IDE or command line/terminal to run code.
- (optional) NumPy installed.
How to Add Elements to a Python Array?
Python lists act as array-like data structures that allow different element types. On the other hand, the array and NumPy libraries offer modules to create traditional arrays.
The methods to add an element to an array differ depending on the data type. Below is an overview of several possible methods.
Method 1: Adding to an Array in Python by Using Lists
If using lists as arrays, Python offers several built-in methods to add one or more elements to an existing list. The methods are:
Below is an overview of the three different methods with examples.
1. Add one or more elements to the end of a list with the ( + ) operator. The operator adds a single value or another list (or iterable) to an existing list and updates the original variable.
To append one element to a list, see the following example:
l = [1, 2, 3] print("List before adding:", l) l = l + ["four"] print("List after adding:", l)
Alternatively, append multiple elements:
l = [1, 2, 3] print("List before adding:", l) l = l + ["four", "five", "six"] print("List after adding:", l)
The operator appends the elements to the end of the original list and increases the list length.
Note: Read our article and find out how to append a string in Python.
2. Add a single element to the end of a list with the built-in append() method. The method does not support adding multiple values to a list.
For example, the code below shows how to create a list and append an element:
l = [1, 2, 3] print("List before appending:", l) l.append("four") print("List after appending:", l)
The original list contains the new element at the end.
3. Add one or more elements to the end of a list with the built-in extend() method. Provide a new list (or another iterable) as the method’s parameter.
The example below shows how to add a new list to an existing one:
l = [1, 2, 3] print("List before extending:", l) l.extend(["four", "five", "six"]) print("List after extending:", l)
The old list extends with the new elements, which all append to the end of the list.
4. Add an element to an exact location in a list with the insert() method. Provide the location index and the value to insert.
l = [1, 2, 3] print("List before inserting:", l) l.insert(1, 1.5) print("List after inserting:", l)
The new element inserts at the provided index, while all the following values shift to the right by one index.
Method 2: Adding to an Array in Python by Using Array Module
The Python array module mimics traditional arrays, where all elements are restricted to the same data type. The module is similar to lists when it comes to adding new elements. The possible methods are:
Below is a brief overview of each method using the array module.
1. Add one or more elements to an array using the + operator. The element(s) append to the end.
The example below shows how to add to an array with the + operator:
import array a = array.array('i', [1,2,3]) print("Array before adding:", a) b = array.array('i', [4,5,6]) a = a + b print("Array after adding", a)
Both arrays use the same type code ( ‘i’ for unsigned integer). The + operator appends the new set of numbers to the existing one.
2. Add a single element to an array with the append() method. The method appends a new value to the end, which must match the array element type.
See the following example to use the append() function to add a new element to an array:
import array a = array.array('i', [1,2,3]) print("Array before appending:", a) a.append(4) print("Array after appending:", a)
The method extends the existing list with a new element, which matches the array type code.
3. Add one or more elements to an array end with the extend() method. Provide the elements in an iterable data type. The items in the iterable must match the array’s data type.
For example, add multiple elements to an array with a Python list:
import array a = array.array('i', [1,2,3]) print("Array before extending:", a) a.extend([4,5,6]) print("Array after extending:", a)
The extend() method links the list to the array.
4. Add an element to a specific place in an array with the insert() method. Set the element index and provide the element value to insert the value.
import array a = array.array('i', [1,2,3]) print("Array before inserting:", a) a.insert(1,0) print("Array after inserting:", a)
The value 0 inserts at index 1 , pushing all the following elements to the right.
Method 3: Adding to NumPy Array
The NumPy library contains an array type suitable for numerical calculations. Unlike lists and arrays, fewer ways exist to add elements to a NumPy array. The methods are:
Both methods work as functions that require the array and appended values.
Below are explanations and examples of both methods for inserting elements into a NumPy array.
1. Use the append() method to add one or more elements to a copy of a NumPy array. Provide the original variable and add the new values to an iterable.
import numpy as np n = np.array([1, 2, 3]) print("NumPy array before appending:", n) n = np.append(n, [4, 5, 6]) print("NumPy array after appending:", n)
The append() method adds a list to the NumPy array and overwrites the old variable with new values.
2. Use the insert() method to add one element at a specific location to a copy of a NumPy array. Provide the NumPy array, index, and value to add a new element.
import numpy as np n = np.array([1,2,3]) print("NumPy array before inserting:", n) n = np.insert(n,1,0) print("NumPy array after inserting:", n)
The method inserts a new value ( 0 ) at index 1 and pushes all following values by one to the right.
After reviewing the methods from this guide, you know how to add an element to an array.
Whether you’re working with lists, arrays, or NumPy arrays, Python offers several methods for each data type to insert new values.
Python add elements to an Array
Python doesn’t have a specific data type to represent arrays.
The following can be used to represent arrays in Python:
Upskill 2x faster with Educative
Supercharge your skillset with Educative Python courses → use code: ASK15 to save 15%
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)
[‘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)
[‘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 ') number = int(input("Please enter a number to be added:\n")) index = int(input(f'Enter the index between 0 and to add the given number:\n')) my_input.insert(index, number) print(f'Updated List ')
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.
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)
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.
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)
[ 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