Arrays in Python are very similar to Lists. Similar to lists, arrays are indexed based, sequential data structure. But arrays are homogeneous, type-code specific and fixed size collection of elements. Python (without NumPy) supports only one-dimensional and numeric elements in arrays. Because of these features arrays are more efficient, memory optimized compared to lists.
Concatenation of Arrays
Similar to lists in Python, array supports multiple ways for concatenation. Almost all methods which support concatenation of lists can be used for concatenation of arrays as well. Lets these methods working with concatenation of arrays.
With arrays, we can concatenate arrays which are created with same type-code. Arrays created with different type-codes will not be merged and will given an error.
+
‘ operator
Concatenate using ‘Using ‘+’ operator is the simplest method to concatenate two or more arrays. It is like extension of one array elements with another array elements. Following is the syntax:
concatenated_array = array_1 + array_2
- ‘
+
’ operator will merge all the elements of all the arrays given for concatenation. - All elements of arrays will be merged in the same order, as given with ‘+’ operator. In this case,
array_1
elements will be first and thenarray_2
elements will be merged in theconcatenated_array
. - Original arrays will remain same. Like there will be no change in
array_1
andarray_2
elements.
# concatenate array using '+' operator
print("concatenate array using '+' operator")
import array as array
signed_short_array = array.array("h", [54, 22, -35, 89]) # defined array type-code 'h' signed short
print(signed_short_array)
concat_array = signed_short_array + array.array("h", [-35, 69]) # concatenate array using '+' operator
print(concat_array)
In this program, we have defined an array signed_short_array
with type-code h (signed short)
. We are using +
operator to concatenate another array array.array("h", [-35, 69])
with signed_short_array
. Both arrays to be merged are of same type-code. Concatenated array concat_array
should have all the elements form both arrays.
Program Output
concatenate array using '+' operator
array('h', [54, 22, -35, 89]) # original array elements
array('h', [54, 22, -35, 89, -35, 69]) # concatenated array elements
From the output, concatenated array has all the elements from both arrays. Final concatenated array is of type-code 'h' (signed short)
. No change in elements of original array.
operator
plugin
Concatenate using We can use Python module operator
to concatenate the arrays. operator
is Python’s built-in module. Following is the syntax:
concatenated_array = operator.add(array_1, array_2)
operator.add(array_1, array_2)
will merge all the elements of all the arrays given for concatenation.- All elements of arrays will be merged in the same order, as given with
operator.add()
method. - Original arrays will remain same. Like there will be no change in
array_1
andarray_2
elements.
# Concatenate using operator plugin
print("Concatenate using operator plugin")
import array as array # import array module
import operator # Import operator module
a = array.array("i", [5,6,8]) # array with type-code i (signed integer)
b = array.array("i", [50,60,80])
c = operator.add(a,b) # Concatenate array using operator.add method
print("a - ", a)
print("b - ", b)
print("c - ", c) # Print concatenated array
Before using any module, we need to import that module in the program. In this program we are importing array
and operator
modules. We are defining 2 arrays which are referenced by variables a
and b
. Both arrays are of type-code i (signed integer)
. We are using operator.add()
method to concatenate arrays and the concatenated array is referenced by variable c
.
Program Output
Concatenate using operator plugin
a - array('i', [5, 6, 8])
b - array('i', [50, 60, 80])
c - array('i', [5, 6, 8, 50, 60, 80]) # concatenated array with type-code i (signed integer)
From the output, concatenated array c
has elements merged from both arrays. Elements from array a
are first and then elements of array b
are merged. No change in elements of arrays a
and b
.
itertools.chain()
Concatenate using We can use Python module itertools
to concatenate the arrays. itertools
is Python’s built-in module. This module is used to iterate through the collections like arrays, lists, tuples, sets, dictionary etc.
itertools.chain()
function is used to iterate through the array elements. This function also returns an iterator. Syntax to concatenate arrays using itertools.chain()
concatenated_array = array.array(itertools.chain(array_1, array_2))
itertools.chain()
method will return an iterator, to iterate through all element of the arrays.- We are using the
array.array()
method to construct an array through the iterated elements. - All elements of both arrays will be iterated and collected in the given order
- Original arrays will remain same. Like there will be no change in
array_1
andarray_2
elements.
# Concatenate using itertools.chain()
print("Concatenate using itertools.chain()")
import array as array # import array module
import itertools # Import itertools module
x = array.array("i", [5,6,8])
y = array.array("i", [50,60,80])
z = array.array("i", itertools.chain(x, y)) # Concatenate array using itertools.chain
print("x - ", x)
print("y - ", y)
print("z - ", z) # Print concatenated array
We need to import array
and itertools
module in the program. We are using itertools.chain()
method to iterate through the elements of both the arrays. Both arrays are of same type-code i (signed integer)
. Iterated elements are collected and using array.array()
new concatenated array is created. This gives us third concatenated array reference with variable z
.
Program Output
Concatenate using itertools.chain()
x - array('i', [5, 6, 8])
y - array('i', [50, 60, 80])
z - array('i', [5, 6, 8, 50, 60, 80])
From the output, concatenated array z
has elements merged from both arrays x
and y
. Elements from array x
are first and then elements of array y
are merged. No change in elements of arrays x
and y
.
extend()
method
Concatenate using Arrays in Python has extend()
method to extend (merge) the elements of a array with another array. Syntax of using extend()
method:
array_1.extend(array_2)
- Using
extend()
method, elements ofarray_2
will be added toarray_1
extend()
method does not return any other array.array_1
will be changed and extended witharray_2
elements
# extend method - extend array
print("extend method - extend array")
import array as array
integer_array = array.array("i", [10, 20, 30, 40]) # defined array, type-code i (signed integer)
print(integer_array)
integer_array.extend(array.array("i", [50, 60])) # extend array with another array
print(integer_array)
In this program, we have defined an array integer_array
with type-code i (signed integer)
. Using integer_array.extend(array.array("i", [50, 60]))
we are extending this array with another array of same type-code elements. Second array elements will be merged with integer_array
.
Program Output
extend method - extend array
array('i', [10, 20, 30, 40])
array('i', [10, 20, 30, 40, 50, 60]) # extended array
from the program output, all elements from both arrays got merged and extended to integer_array
.
[*k, *l]
Concatenate – packing & unpacking In python astrisk (*)
is used to pack & unpack the elements of a collection. We can use astrisk (*) to unpack the elements of multiple arrays and to collect multiple array elements into a single array. Syntax of using astrisk (*):
concatenated_array = array.array(*array_1, *array_2)
- Using
astrisk (*)
, elements ofarray_1
andarray_2
are unpacked and then collected into concatenated array. - Original arrays will remain same. Like there will be no change in
array_1
andarray_2
elements.
# unpacking - (*k, *l) - using asterisk*
print("unpacking - [*k, *l] - using asterisk*")
import array as array
k = array.array("i", [5,6,8])
l = array.array("i", [50,60,80]) # Defined arrays k and l, type-code i (signed integer)
m = array.array("i", [*k , *l]) # unpack and collect elements
print("k - ", k)
print("l - ", l)
print("m - ", m) # Print concatenated array
Using [*k , *l]
– Two arrays k
and l
are unpacked and then collected again as a array using array.array()
method.
Program Output
unpacking - [*k, *l] - using asterisk*
k - array('i', [5, 6, 8])
l - array('i', [50, 60, 80])
m - array('i', [5, 6, 8, 50, 60, 80]) # concatenated array of type-code i (unsigned integer)
TypeError
with different type-code
We have explored multiple ways to concatenate elements from two or more arrays. But arrays were merged only when all arrays were of same type-code. Otherwise it will raise (throw) TypeError
.
# TypeError - array with different type-code
print("TypeError - array with different type-code")
import array as array
signed_short_array = array.array("h", [54, 22, -35, 89]) # array with type code h (signed short)
concat_array = signed_short_array + array.array("H", [35, 69]) # merge array with type-code H (unsigned short)
In this program, we have defined an array signed_short_array
with type-code h (signed short)
. Using signed_short_array + array.array("H", [35, 69])
we are merging two arrays with +
operator. Second array to be merged is of type-code H (unsigned short)
. Two arrays of different type-codes cannot be merged and will raise an TypeError
.
Program Output
TypeError - array with different type-code
Traceback (most recent call last):
File "D:\concatenate-array.py", line 83, in <module>
concat_array = signed_short_array + array.array("H", [35, 69])
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: bad argument type for built-in operation
From the program output, Two arrays of different type-codes cannot be merged and we got an error TypeError: bad argument type for built-in operation
Summary
In this article we explored various methods to concatenate two or more array elements. Following topics were discussed: