Last updated: Apr 8, 2024
Reading timeยท4 min
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.
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.
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.
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:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range 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.
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.
for
loopIf you need to create a list by repeating multiple values N times, use a for
loop.
a_list = [1, 2, 3] new_list = [] count = 3 for index in range(count): for item in a_list: new_list.append(item) # ๐๏ธ [1, 2, 3, 1, 2, 3, 1, 2, 3] print(new_list)
We used a for
loop to iterate over a range
object 3 times.
On each iteration, we use a nested for
loop to iterate over the list and add
its values to the new list using append()
.
This is a three-step process:
itertools
module.repeat()
method, e.g. itertools.repeat('a', 3)
.list()
class to convert the object to a list.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:
Name | Description |
---|---|
object | The object to be returned from the iterator |
times | How 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.
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.
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.
If you need to append an item to a list N times:
range
object.list.extend()
method to extend the original list with the value
repeated N times.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.
The list.extend() method takes an iterable and extends the list by appending all of the items from the iterable.
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.
You can learn more about the related topics by checking out the following tutorials: