Append an item to a list N times in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Append an item to a list N times in Python #

Use the multiplication operator to append an item to a list N times, e.g. my_list = ['a'] * 5. The multiplication operator will return a list containing the specified value repeated N times.

main.py
# ✅ using multiplication operator my_list = ['a'] * 5 print(my_list) # 👉️ ['a', 'a', 'a', 'a', 'a'] # ------------------------------ # ✅ using list comprehension my_list = ['a' for _ in range(5)] print(my_list) # 👉️ ['a', 'a', 'a', 'a', 'a'] # ------------------------------ # ✅ using list.extend() my_list = ['a', 'b'] my_list.extend('c' for _ in range(3)) print(my_list) # 👉️ ['a', 'b', 'c', 'c', 'c']

The first example uses the multiplication operator to append a value to a list N times.

main.py
my_list = ['a'] * 5 print(my_list) # 👉️ ['a', 'a', 'a', 'a', 'a']
When the multiplication * operator is used with a list and an integer, a new list containing the items of the original list repeated N times is returned.
main.py
print(['a', 'b'] * 2) # 👉️ ['a', 'b', 'a', 'b'] print(['a'] * 2) # 👉️ ['a', 'a']

Alternatively, you can use a list comprehension.

Append an item to a list N times using a list comprehension #

To append an item to a list N times:

  1. Use a list comprehension to iterate over a range object.
  2. Return the value on each iteration.
  3. The new list will contain the specified value repeated N times.
main.py
my_list = ['a' for _ in range(5)] print(my_list) # 👉️ ['a', 'a', 'a', 'a', 'a']

We used a list comprehension to iterate over the range object.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The range() class creates a range object that is commonly used for looping a specific number of times.

main.py
for n in range(5): print(n) # 👉️ 0 1 2 3 4 result = list(range(5)) # 👇️ [0, 1, 2, 3, 4] print(result)

Alternatively, you can use the list.extend() method.

Append an item to a list N times using list.extend() #

To append an item to a list N times:

  1. Use a generator expression to iterate over a range object.
  2. Use the list.extend() method to extend the original list with the value repeated N times.
main.py
my_list = ['a', 'b'] my_list.extend('c' for _ in range(3)) print(my_list) # 👉️ ['a', 'b', 'c', 'c', 'c']

We used a generator expression to iterate over the range object.

Generator expressions are very similar to list comprehensions.

They are used to perform some operation for every element or select a subset of elements that meet a condition.

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.

This approach is useful when you need to append an item N times to an existing list.

The multiplication operator should be your preferred approach if you need to create a list that contains the same value repeated N times.

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.