- Add Elements to an Array in Python
- 1. Quick Examples of Add Elements to an Array
- 2. Add Element to an Array Using Lists
- 2.1 Using append() Method
- 2.2 Using + Operator
- 2.3 Using insert() Method
- 2.4 Using extend() Method
- 3. Add Element to an Array Using Array Module
- 4. Add Element to NumPy Array
- Conclusion
- You may also like reading:
- How to add all numbers in a list in Python
- How can you add all numbers in a list in Python
- sum()
- Slicing
Add Elements to an Array in Python
How to add elements to an array in python? Python does not have a built-in array data type, but you can use lists , the array module , or the NumPy module to represent arrays. You can add elements to an array in python by using many ways, for example, using the + operator, append() , insert() , and extend() functions. In this article, I will explain add elements to an array in python using all these methods with examples.
Below are methods to add elements to an array in python:
- Using Lists – A list is a built-in data type in Python that can be used to represent arrays. Lists are very flexible and can contain elements of different data types.
- Using the array module – The array module is a built-in module in Python that provides a way to represent arrays of a specific data type.
- Using the NumPy module – NumPy is a popular third-party library for scientific computing in Python. It provides a powerful N-dimensional array object that can be used to represent arrays.
1. Quick Examples of Add Elements to an Array
If you are in a hurry, below are some quick examples of how to add elements to an array in python.
# Quick examples of add elements to an array # Example 1: Add an element to the list # Using append() method technology = ['Spark','Python','Pyspark'] technology.append('Pandas') # Example 2: Use + operator numbers = [2, 4, 6] numbers = numbers + [8] # Example 3: Using insert() method technology = ['Spark','Python','Java'] technology.insert(0, 'Pandas') # Example 4: Use extend() function technology = ['Pyspark', 'Python'] technology1 = ['Java', 'spark'] technology.extend(technology1) # Example 5: Add element to an array # Using array module numbers = array.array('i', [0, 5, 10, 15]) numbers.append(20) # Example 6: Array module # Using insert() method numbers = array.array('i', [0, 4, 6, 8]) numbers.insert(1, 2) # Example 7: Use extend() method # to array module arr = array.array('i', [5, 10, 15]) mylist = [20, 25, 30] arr.extend(mylist) # Example 8: Ad the two arrays # Using the + operator arr = array.array('i', [10, 20, 30]) arr1 = array.array('i', [40, 50, 60]) result_array = arr + arr1 # Example 9: Add element to numpy array # Using numpy.append() method arr = np.array([0, 2, 4, 6]) app_arr = np.append(arr, 8) # Example 10: Using numpy.insert() method arr = np.array([10, 20, 30]) arr1 = np.insert(arr, 0, 5)
2. Add Element to an Array Using Lists
- Using append() method
- Using + operator
- Using insert() method
- Using extend() method
2.1 Using append() Method
You can use the list append() function to add an element or list of elements in Python. For example, For example, you create a list technology containing the elements [‘Spark’,’ Python’,’Pyspark’] . You then call the append() method on the list and pass in the element you want to add, in this case, pandas . The append() method adds the element to the end of the list.
# Add an element to the list technology = ['Spark','Python','Pyspark'] print("Actual List",technology) # Add 'Java' to the list technology.append('Pandas') print("Updated List: ",technology) # Output # Actual List ['Spark', 'Python', 'Pyspark'] # Updated List: ['Spark', 'Python', 'Pyspark', 'Pandas']
2.2 Using + Operator
The + operator can be used to concatenate two lists, effectively adding the elements from one list to the end of another. For example, you create a list numbers containing the elements [2, 4, 6] . you then concatenate the list with another list containing the element you want to add, in this case [8] . The + operator combines the two lists, effectively adding elements 8 to the end of the original list.
# Use + operator numbers = [2, 4, 6] numbers = numbers + [8] print(numbers) # Output # [2, 4, 6, 8]
2.3 Using insert() Method
The insert() method can be used to add an element to a specific position in the list.
You can use the insert() to add an element at a specific index in the python list. For example, you can insert ‘Pandas’ it at the first position on the list. For more examples refer to add element to list by index.
# Using insert() method technology = ['Spark','Python','Java'] # Insert first position technology.insert(0, 'Pandas') print(technology) # Output # ['Pandas', 'Spark', 'Python', 'Java']
2.4 Using extend() Method
The list.extend() adds the elements to the existing list at the end. When you add an iterator by using this method, it actually extends the elements as separate elements and adds them to the list. Note that this modifies the existing list with the new elements and the elements are added at the end of the original list. For more examples refer to append multiple elements to the list.
# Use extend() function technology = ['Pyspark', 'Python'] technology1 = ['Java', 'spark'] # Extend list to list technology.extend(technology1) print(technology) # Output # ['Pyspark', 'Python', 'Java', 'spark']
3. Add Element to an Array Using Array Module
To add an element to an array using the array module in Python, you can use the append() method of the array object. For example, you first create an array numbers of integers using the ‘i’ type code. you then use the append() method to add the integer 20 to the end of the array.
import array # Create an array of integers numbers = array.array('i', [0, 5, 10, 15]) # Append an integer to the end of the array numbers.append(20) print(numbers) # Output # array('i', [0, 5, 10, 15, 20])
You can also use the insert() method to add an element at a specific index of the array. For example, you use the insert() method to add the integer 2 at the index 1 of the array. The existing elements are shifted to the right to make room for the new element.
# Create an array of integers numbers = array.array('i', [0, 4, 6, 8]) # Array module Using insert() method numbers.insert(1, 2) print(numbers) # Output # array('i', [0, 2, 4, 6, 8])
You can use extend() method in Python’s array module is used to add elements to an array by appending all the elements of another iterable (such as a list, tuple, or another array) to the end of the array. It essentially elongates the array with the elements from the iterable passed as an argument.
# Create an array of integers arr = array.array('i', [5, 10, 15]) # Create a list of integers mylist = [20, 25, 30] # Use extend() method to array module arr.extend(mylist) print(arr) # Output # array('i', [5, 10, 15, 20, 25, 30])
Similarly, you can also use + operator to add two arrays in Python’s array module. It combines the elements of both arrays to create a new array. For example, you first create two arrays arr and arr1 of integers using the ‘i’ type code. you then use the + operator to concatenate the two arrays and create a new array result_array that contains all the elements of both arrays.
# Create two arrays of integers arr = array.array('i', [10, 20, 30]) arr1 = array.array('i', [40, 50, 60]) # Ad the two arrays # Using the + operator result_array = arr + arr1 print(result_array) # Output # array('i', [10, 20, 30, 40, 50, 60])
4. Add Element to NumPy Array
To add elements to a NumPy array in Python, you can use the append() function provided by the NumPy module. For example, you first create a NumPy array arr of integers using the array() function provided by the NumPy module. you then use the append() function to add the integer 8 to the end of the array.
import numpy as np # Create a NumPy array of integers arr = np.array([0, 2, 4, 6]) # Add element to numpy array # Using numpy.append() method app_arr = np.append(arr, 8) print("Appended array:\n",app_arr) # Output # Appended array: [0 2 4 6 8]
You can also use the insert() function to add an element at a specific index of the array. For example, you can use the insert() function to add the integer 5 at index 0 of the array.
# Create a NumPy array of integers arr = np.array([10, 20, 30]) # Using numpy.insert() method arr1 = np.insert(arr, 0, 5) print(arr1) # Output # [ 5 10 20 30]
Conclusion
In this article, I have explained how to add elements to an array in python by using the + operator, append() , insert() , and extend() functions with examples.
You may also like reading:
How to add all numbers in a list in Python
Hi everyone, today in this tutorial let us see how to add all numbers in a list in Python. Basically, we can add numbers in a list in many ways, like in other programming languages we can use loops(for loop, while loop). We can also use lambda.
But in our tutorial let us see how to add elements in a list using built-in function and slicing. This way of doing is better than using other methods. This is because slicing is an important technique in machine learning.
How can you add all numbers in a list in Python
Let us now see how to add all numbers in a list using a built-in function. We use sum() which is a built-in function in Python programming language.
sum()
The sum() is a built-in function as said earlier. This function will work only when numeric values are given to it, this will not work when alphabets are given to it or when numbers are given in the form of string it will show errors. This function basically sums up the numbers that are given to this and return the sum of numbers given in it.
Let us now see the usage of for loop in sum() function.
p=[2,3,5] print(sum(i for i in p))
You can also use the sum() function to add a constant value for the sum total of the list. The syntax of the sum() function is sum(iterable, start). So here, start means a value that can be summed up to the list and iterable means list, tuple, dict, etc.,
Now, let us see how to add a list of numbers using the slicing method.
Slicing
This slicing is an amazing feature given to us in Python language. This way of writing the code and analyzing the data plays a key role in Machine learning and Deep learning techniques.
Let us now have a look on our code.