Python Append List To Another List


In this tutorial, you are going to learn how Python append list to another list.

While working with Python programs you will often need to append a list to another list. For example, if you have a list of names and you want to add a new name to the list or you want to add a list of transactions to another transaction list.

# Quick Solution

The quick solution is to use the extend() method if you want to make an individual element of another list be added as a single element to the list.

Although we are going to discuss 6 different ways to append a list to another list to make it a list of lists. Here is the list of all the ways:

    Table Of Contents

  1. Append list using extend() method
  2. Append the list by concatenating
  3. Append list using loop
  4. Using itertools.chain() Method
  5. Append list using slicing
  6. Using unpacking generalizations
Python append list to another list

1. Append list using extend() method

extend() is the most used method to append a list to another list. It takes every single element of the other list and adds it to the end of the list as an individual element.

The method is called on the list and takes the other list as an argument. For example list1.extend(list2).

The following example shows how to append a list to another list using extend() method.

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# Append list2 to list1
list1.extend(list2)
print(list1)

Output:

[1, 2, 3, 'a', 'b', 'c']

You can also directly pass another list within the method itself.

list1 = [1, 2, 3]

list1.extend([4, 5, 6])
print(list1)

Output:

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

2. Append list by concatenating

In python, you can concatenate a list by using the + operator. For example, list1 + list2.

Using this operator 2 or more lists can be concatenated into a single list and all the elements of another list will be elements of the new list.

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# Concatenate list1 and list2
list3 = list1 + list2
print(list3)

Output:

[1, 2, 3, 'a', 'b', 'c']

The + operator concatenated 2 lists into a single list.


3. Append list using loop

You know that if you append a list to another list using the append() method then the other list will be added as a whole to the end of the list. Making the whole list the last element of the original list.

Since we want to append a list to another list like the extend() method does, we can use a loop and append elements one by one.

By appending elements one by one every element of the other list will be an element of the original list.

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# using for loop with append() method
for i in list2:
    list1.append(i)

print(list1)

Output:

[1, 2, 3, 'a', 'b', 'c']

4. Using itertools.chain() Method

The itertools is a module that contains a lot of useful functions. The chain() method is one of them. It takes multiple lists as an argument and returns a single list.

You can use the chain() method to append a list to another list.

from itertools import chain

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# using chain method
list3 = list(chain(list1, list2))
print(list3)

Output:

[1, 2, 3, 'a', 'b', 'c']

You can also pass more than 2 lists in the chain() method.

from itertools import chain

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = [4, 5, 6]

list4 = list(chain(list1, list2, list3))
print(list4)

Output:

[1, 2, 3, 'a', 'b', 'c', 4, 5, 6]

5. Append list using slicing

The slicing assignment is used to assign a slice of a list to another list. This way you can replace a part of a list with another list.

For example:

a = [1, 2, 3, 4, 5]
b = ['a', 'b']

a[2:4] = b
print(a) # [1, 2, 'a', 'b', 5]

a[1:3] = [0, 0]
print(a) # [1, 0, 0, 'b', 5]

This way can also be used to append a list to another list.

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# using slicing assignment
list1[len(list1):] = list2
print(list1)

Output:

[1, 2, 3, 'a', 'b', 'c']

Code explanation: In the code above we are able to append list2 to list1 by using len(lis1) as the start index. When setting the start index to len(list1), it will start to append the list2 to the end of the list1, keeping the original list1 intact. When the end index is not specified, it will append the whole list2 to the end of the list1.


6. Using Unpacking Generalizations (*)

The unpacking generalizations are used to unpack a list into multiple variables. You can unpack a list into multiple variables by using * operator.

It is available only in python3 above version 3.5.

For example if list1 = [1, 2, 3] and list2 = ['a', 'b', 'c'] then list3 = [*list1, *list2] will be list3 = [1, 2, 3, 'a', 'b', 'c'].

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# unpacking generalizations
# unpack both lists in a list literal
list3 = [*list1, *list2]

print(list3)

Output:

[1, 2, 3, 'a', 'b', 'c']

Conclusion

The extend() method is the most straightforward way to append a list to another list. However, there are 5 other methods discussed in this article of how python append list to another list.