It is important to know the difference between extend and append when working with arrays in Python.

Let’s say we have an example array: my_array = [].

If an array is passed to append, (i.e. my_array.append([1, 2]), then my_array will become a 2D array. It will look like this: [[1,2]]. In other words, append won’t flatten the arrays together into a 1D array.

However, it is possible to combine two arrays like this and end up with a 1D array using Python’s extend method. Calling my_array.extend([1, 2]) will result in my_array looking like this: [1,2].

Here is an example using the Python REPL:

>>> a = [1]
>>> b = [1, 2]
>>> c = [1, 2, 3]
>>> a.append(b)
>>> a
[1, [1, 2]]
>>> c.extend(b)
>>> c
[1, 2, 3, 1, 2]

Python 3.5 introduced another way to combine arrays. The unpacking operator * can be used in the following way:

>>> [1, 2, 3, *[4, 5, 6]]
[1, 2, 3, 4, 5, 6]

Note, the unpacking approach does not work in Python 2.7.14.