# Appending two arrays together in python

When working with arrays in Python, there may be times when you need to combine or append two arrays together. This can be done in several ways, each with its own advantages and disadvantages. In this article, we will explore three different methods to solve the problem of appending two arrays together in Python.

## Method 1: Using the + operator

One simple way to append two arrays together is by using the + operator. This operator concatenates the two arrays, creating a new array that contains all the elements from both arrays.

``````array1 = [1, 2, 3]
array2 = [4, 5, 6]

result = array1 + array2

print(result)``````

In this example, we have two arrays, `array1` and `array2`. By using the + operator, we can concatenate these two arrays and store the result in the `result` variable. The output will be `[1, 2, 3, 4, 5, 6]`.

## Method 2: Using the extend() method

Another way to append two arrays together is by using the `extend()` method. This method adds all the elements from one array to the end of another array.

``````array1 = [1, 2, 3]
array2 = [4, 5, 6]

array1.extend(array2)

print(array1)``````

In this example, we have two arrays, `array1` and `array2`. By using the `extend()` method on `array1` and passing `array2` as an argument, we can append the elements of `array2` to the end of `array1`. The output will be `[1, 2, 3, 4, 5, 6]`.

## Method 3: Using the append() method in a loop

If you have multiple arrays that you want to append together, you can use the `append()` method in a loop to add each array to a new array.

``````array1 = [1, 2, 3]
array2 = [4, 5, 6]
array3 = [7, 8, 9]

result = []

for array in [array1, array2, array3]:
result.extend(array)

print(result)``````

In this example, we have three arrays, `array1`, `array2`, and `array3`. We create an empty array called `result` and then use a loop to iterate over each array. Inside the loop, we use the `extend()` method to append each array to the `result` array. The output will be `[1, 2, 3, 4, 5, 6, 7, 8, 9]`.

After exploring these three methods, it is clear that using the + operator is the simplest and most concise way to append two arrays together in Python. It requires less code and is easier to understand. Therefore, the + operator method is the recommended option for appending arrays in Python.

Rate this post

### 11 Responses

1. Milo Krueger says:

Method 3 is too messy, Ill stick with Method 1s simplicity.

1. Estrella Miller says:

I couldnt disagree more. Method 3 may require a bit of effort, but the results are worth it. Method 1 might be simple, but it lacks the effectiveness that Method 3 offers. Trust me, give it a try and youll see the difference.

2. Bjorn says:

Method 4: Using a magical unicorn to merge arrays. Trust me, it works like charm! 🦄✨

3. Angie Rubio says:

Method 4: Why not try using list comprehension to append arrays? 🤔🔥 #PythonMagic

4. Warren Mckinney says:

Method 1 is the bomb! Simple and clean, just how I like it. #PythonPower

5. Braelyn says:

Method 4: How about using a magic wand to append arrays? ✨🪄 No? Okay, just checking!

6. Rachel Grant says:

Method 3 seems like a hassle, why not just use the simpler extend() method?

7. Ahmir Reed says:

Method 4: Why not use the itertools.chain() function? Its fast, concise, and elegant!

8. Alexis says:

Method 1 using the + operator is concise, but Method 3 using append() in a loop feels more flexible. What do you think?

1. Gunner Briggs says:

I completely disagree. Method 1 is far more efficient and readable. Why complicate things with a loop and append()? Keep it simple and stick with the + operator. Trust me, its the way to go.

9. Savanna says:

Method 4: Using a magical spell to merge arrays. Who needs Python when you have sorcery? #Abracadabra