Python appending array to an array

You can append the elements of one list to another with the “+=” operator. Note that the “+” operator creates a new list.

a = [1, 2, 3]
b = [10, 20]

a = a + b # Create a new list a+b and assign back to a.
print a
# [1, 2, 3, 10, 20]


# Equivalently:
a = [1, 2, 3]
b = [10, 20]

a += b
print a
# [1, 2, 3, 10, 20]

If you want to append the lists and keep them as lists, then try:

result = []
result.append(a)
result.append(b)
print result
# [[1, 2, 3], [10, 20]]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)