Combining arrays is a common operation in Python, especially when working with big datasets. In this tutorial, we will show you how to combine 3 arrays in Python.
Steps
Step 1: Define the arrays
Let’s create 3 arrays a, b, and c.
1 2 3 |
a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] |
Step 2: Combine the arrays
There are a few ways to combine arrays in Python. We will use the extend method to add b and c to a.
1 2 |
a.extend(b) a.extend(c) |
Step 3: Print the merged array
Let’s print the merged array to make sure the operation was executed correctly.
1 |
print(a) |
Conclusion
In this tutorial, we learned how to combine 3 arrays in Python using the extend method. This method works well when you have a small number of arrays to combine. If you have a larger number of arrays, you may want to consider using the concatenate method. Overall, combining arrays is a useful operation that can help simplify your code and make it more efficient.
Here is the full code:
1 2 3 4 5 6 7 8 |
a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] a.extend(b) a.extend(c) print(a) |
The output of the code is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]