How to append multiple Values to a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Append multiple values to a List in Python
  2. Insert multiple elements into a List at index in Python

# Append multiple values to a List in Python

Use the list.extend() method to append multiple values to a list.

The list.extend() method will append the items of the provided iterable to the list.

main.py
a_list = ['bobby'] a_list.extend(['hadz', '.', 'com']) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

append multiple values to list

The code for this article is available on GitHub

If you need to insert multiple elements into a list at a specific index, click on the following subheading:

The list.extend() method takes an iterable and extends the list by appending all of the items from the iterable.

The list.extend() method returns None as it mutates the original list.

We passed a list containing multiple values to the extend() method, but you can pass it any other iterable, e.g. a tuple or a range object.
main.py
a_list = ['bobby'] a_tuple = ('hadz', '.', 'com') a_list.extend(a_tuple) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

Here is an example that uses a tuple.

main.py
a_list = ['bobby'] a_tuple = ('hadz', '.', 'com') a_list.extend(a_tuple) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

# Append multiple values to a List using a for loop

This is a two-step process:

  1. Use a for loop to iterate over the collection of values.
  2. Use the list.append() method to append each value to the list.
main.py
list1 = ['bobby'] list2 = ['hadz', '.', 'com'] for item in list2: list1.append(item) print(list1) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

append multiple values to list using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the collection of values.

On each iteration, we use the list.append() method to append the current value to the list.

# Append multiple values to a List using addition (+) operator

Alternatively, you can use the addition (+) operator.

When the addition (+) operator is used with two lists, it combines the lists into a single list.

main.py
a_list = ['bobby'] multiple_values = ['hadz', '.', 'com'] a_list = a_list + multiple_values print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

append multiple values to list using addition operator

The code for this article is available on GitHub

When the addition (+) operator is used with lists, it combines the lists into a single list.

main.py
print([1] + [2, 3, 4]) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4]
However, combining the two lists and reassigning the original list is a bit slower than using the list.extend() method directly.

# Append multiple values to a List using itertools.chain()

Alternatively, you can use the itertools.chain() method.

The itertools.chain() class can be used to treat multiple lists as a single list.

main.py
from itertools import chain a_list = ['bobby'] multiple_values = ['hadz', '.', 'com'] a_list = list(chain(a_list, multiple_values)) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']
The code for this article is available on GitHub

The itertools.chain() class is used to treat multiple lists as a single list.

main.py
import itertools first = [1, 2] second = [3, 4] third = [5, 6] for item in itertools.chain(first, second, third): print(item) # ๐Ÿ‘‰๏ธ 1 2 3 4 5 6

The itertools.chain() class creates an iterator from the provided iterables.

The class is used for treating multiple sequences as a single sequence.

Which approach you pick is a matter of personal preference. I'd use the list.extend() method as it is quite performant and easy to read.

Another simple approach to append multiple values to a list is to use a for loop.

# Insert multiple elements into a List at index in Python

Use list slicing assignment to insert multiple elements into a list at a specific index, e.g. list1[1:1] = list2.

The elements of the second list will get inserted into the first at the given index.

main.py
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1[1:1] = list2 print(list2) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']
The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.
main.py
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1[0:0] = list2 print(list2) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']

The assignment adds the items of the second list to the first list at the specified index.

If you need to insert multiple elements at the end of a list, use the list.extend() method.

main.py
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1.extend(list2) print(list1) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

The list.extend() method takes an iterable and extends the list by appending all of the items from the iterable.

main.py
my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

The list.extend method returns None as it mutates the original list.

# Insert multiple elements into a List at index using addition (+) operator

This is a three-step process:

  1. Get a slice of the list up to the specified index.
  2. Get a slice of the list after the specified index.
  3. Use the addition (+) operator to combine the two slices and the elements.
main.py
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] index = 1 list1 = list1[:index] + list2 + list1[index:] print(list1) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']
The code for this article is available on GitHub

When used with lists, the addition (+) operator combines the lists.

main.py
print(['a'] + ['b'] + ['c']) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

The slice list1[:index] starts at index 0 and goes up to, but not including the specified index.

The slice list1[index:] starts at the specified index and goes to the end of the list.

The last step is to use the addition (+) operator to combine the elements of the lists.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev