Create a list with same value repeated N times in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

Create a list with same value repeated N times in Python #

Use the multiplication operator to create a list with the same value repeated N times in Python, e.g. my_list = ['abc'] * 3. The result of the expression will be a new list that contains the specified value N times.

main.py
my_list = ['abc'] * 3 print(my_list) # 👉️ ['abc', 'abc', 'abc']

The item you specify in the list will be contained N times in the new list the operation returns.

Make sure to wrap the value you want to repeat in a list.

One thing to be aware of is - if you use this approach with mutable containers like a list or a dictionary, they all point to the same location in memory and have the same reference.
main.py
result = [[]] * 3 # 👇️ [[], [], []] print(result) result[0].append('a') # 👇️ [['a'], ['a'], ['a']] print(result)

We created a list that contains 3 nested lists. However, note that adding an item to one of the lists, added the item in all 3.

This is because all of the nested lists point to the same location in memory.

One way to avoid this issue is to use a list comprehension.

main.py
result = [[] for i in range(0, 3)] # 👇️ [[], [], []] print(result) result[0].append('a') # 👇️ [['a'], [], []] print(result)

The range class is commonly used for looping a specific number of times in for loops and takes the following parameters:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

You can also specify items in the list, just like we did in the first example, and we still end up creating a list with 3 distinct nested lists.

main.py
result = [['a'] for i in range(0, 3)] # 👇️ [['a'], ['a'], ['a']] print(result) result[0].append('b') # 👇️ [['a', 'b'], ['a'], ['a']] print(result)

You can also use the itertools.repeat() method to create a list that contains the same item N times.

To create a list that contains the same item N times:

  1. Import the itertools module.
  2. Call the repeat() method, e.g. itertools.repeat('a', 3).
  3. Use the list() class to convert the object to a list.
main.py
import itertools my_list = list(itertools.repeat('a', 3)) print(my_list) # 👉️ ['a', 'a', 'a']

The itertools.repeat method is used to make an iterator that returns the provided object N times.

The method takes the following 2 arguments:

NameDescription
objectThe object to be returned from the iterator
timesHow many times the object should get returned from the iterator (optional)

If you don't provide a value for the times argument, the iterator runs indefinitely.

Note that this approach also suffers from the same issue when trying to create a list of mutable objects.

main.py
import itertools my_list = list(itertools.repeat(['a'], 3)) print(my_list) # 👉️ [['a'], ['a'], ['a']] my_list[0].append('b') print(my_list) # 👉️ [['a', 'b'], ['a', 'b'], ['a', 'b']]

You can use a list comprehension to avoid this.

main.py
my_list = [['a'] for i in range(0, 3)] print(my_list) # 👉️ [['a'], ['a'], ['a']] my_list[0].append('b') print(my_list) # 👉️ [['a', 'b'], ['a'], ['a']]

All of the nested lists we created point to a different location in memory, so updating one doesn't mutate the others.

Use the search field on my Home Page to filter through my more than 3,000 articles.