Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Join Arrays (Complete Guide for Beginners)

 In Python, arrays are used to store multiple values of the same data type. Sometimes, you need to combine two or more arrays into a single array. This process is called joining arrays.

Python provides several ways to join arrays such as using +, extend(), loops, and manual methods.

In this tutorial, you will learn all methods to join arrays in Python with clear examples.


Creating Arrays

We use Python’s array module.

import array as arr

array1 = arr.array('i', [10, 20, 30])
array2 = arr.array('i', [40, 50, 60])

print(array1)
print(array2)

1. Join Arrays Using Loop

You can manually join arrays using a loop.

Example

for item in array2:
    array1.append(item)

print(array1)

Output

array('i', [10, 20, 30, 40, 50, 60])

Explanation

  • Each element from array2 is added to array1
  • Simple but manual method

2. Join Arrays Using + Operator (Convert to List Method)

Python arrays do not directly support +, so we convert them.

Example

joined = arr.array('i', list(array1) + list(array2))

print(joined)

Output

array('i', [10, 20, 30, 40, 50, 60])

3. Join Arrays Using extend() (Recommended)

The extend() method is the easiest way to join arrays.

Example

array1.extend(array2)

print(array1)

Output

array('i', [10, 20, 30, 40, 50, 60])

Explanation

  • Adds all elements of array2 into array1
  • Efficient and fast

4. Join Arrays Using List Conversion

You can convert arrays to lists and then back.

Example

joined = arr.array('i', list(array1) + list(array2))

print(joined)

Output

array('i', [10, 20, 30, 40, 50, 60])

5. Join Multiple Arrays

You can join more than two arrays.

Example

array3 = arr.array('i', [70, 80])

joined = arr.array('i', list(array1) + list(array2) + list(array3))

print(joined)

Output

array('i', [10, 20, 30, 40, 50, 60, 70, 80])

6. Join Arrays Using Loop (Multiple Arrays)

Example

arrays = [array1, array2]

result = arr.array('i')

for a in arrays:
    for item in a:
        result.append(item)

print(result)

Real-World Example: Student Marks Merge

import array as arr

class1 = arr.array('i', [85, 90, 78])
class2 = arr.array('i', [88, 92, 80])

all_marks = arr.array('i')

all_marks.extend(class1)
all_marks.extend(class2)

print(all_marks)

Real-World Example: Product Inventory Merge

import array as arr

warehouse1 = arr.array('i', [101, 102, 103])
warehouse2 = arr.array('i', [104, 105, 106])

combined = arr.array('i')

for item in warehouse1:
    combined.append(item)

for item in warehouse2:
    combined.append(item)

print(combined)

Join Methods Comparison

MethodTypeDescription
extend()BestFast and simple
loopManualEducational
+ operatorList-basedRequires conversion
list()FlexibleEasy merging

Common Mistakes

Mistake 1: Using + directly on arrays

❌ Wrong:

array1 + array2

✔ Arrays require conversion


Mistake 2: Forgetting extend()

array1.extend(array2)

✔ Best and recommended method


Mistake 3: Mixing data types

arr.array('i', [10, "hello"])

✔ Arrays must have same type


Safe Join Example

joined = arr.array('i', list(array1) + list(array2))

Array Join Methods Summary

MethodUse
extend()Best method
loopCustom control
list + listFlexible
multiple extend()Clean merging

Practice Exercise 1

Join two arrays using extend().


Practice Exercise 2

Join three arrays into one.


Conclusion

Joining arrays in Python is simple and very useful for combining datasets.

You learned:

  • Different ways to join arrays
  • Best method using extend()
  • Loop-based and conversion methods
  • Real-world use cases

Mastering array joining is essential for data processing, analytics, and real-world applications.




Post a Comment

0 Comments