How to Create Lists with Repeated Values in Python
Creating lists with the same value repeated multiple times is a common task in Python.
This guide covers various methods for generating such lists, including the multiplication operator (*
), list comprehensions, itertools.repeat()
, and using loops. We'll also address the important distinction between repeating immutable and mutable objects.
Using the Multiplication Operator (*
)
The multiplication operator (*
) is the most concise way to create a list with repeated values:
my_list = ['abc'] * 3
print(my_list) # Output: ['abc', 'abc', 'abc']
Repeating Immutable Values
The result is a new list containing N references to the same list object.
This approach works perfectly for immutable values like strings, numbers, and tuples:
numbers = [0] * 5
print(numbers) # Output: [0, 0, 0, 0, 0]
strings = ["hello"] * 3
print(strings) # Output: ['hello', 'hello', 'hello']
tuples = [(1, 2)] * 2
print(tuples) # Output: [(1, 2), (1, 2)]
The Mutability Trap (and How to Avoid It)
Crucially, when you use *
with mutable objects (like lists, dictionaries, or custom class instances), you're creating multiple references to the same object, not copies. Modifying one element will affect all apparent "copies":
result = [[]] * 3 # WRONG for mutable objects!
print(result) # Output: [[], [], []] (Looks correct, but...)
result[0].append('a')
print(result) # Output: [['a'], ['a'], ['a']] (All lists changed!)
- All of the inner lists refer to the same list object. Changing one nested list changes all the others because they're all references to the same memory location.
Using List Comprehensions (for Mutable Objects)
The correct way to create a list of distinct mutable objects is to use a list comprehension:
result = [[] for _ in range(3)] # Creates three *different* empty lists
print(result) # Output: [[], [], []]
result[0].append('a')
print(result) # Output: [['a'], [], []] (Only the first list is modified)
[[] for _ in range(3)]
: This creates a new, empty list[]
three separate times. Each list is independent.- The
_
is used as a variable name because we don't need to use the loop counter value.
You can also add existing items to nested list:
result = [['a'] for i in range(0, 3)]
print(result) # Output: [['a'], ['a'], ['a']]
result[0].append('b')
print(result) # Output: [['a', 'b'], ['a'], ['a']]
Using itertools.repeat()
The itertools.repeat()
function creates an iterator that yields the same value repeatedly. You can use it with list()
to create a list:
import itertools
my_list = list(itertools.repeat('a', 3))
print(my_list) # Output: ['a', 'a', 'a']
itertools.repeat('a', 3)
: Creates an iterator that will yield 'a' three times.list(...)
: Consumes the iterator and builds a list.
itertools.repeat()
has the same mutability issue as the *
operator when used with mutable objects. Use a list comprehension instead for independent mutable objects.
Using Loops and append()
/ extend()
Repeating a Single Value with extend()
If you want to add to an existing list:
my_list = ['a', 'b']
my_list.extend('c' for _ in range(3)) # Creates ['c', 'c', 'c']
print(my_list) # Output: ['a', 'b', 'c', 'c', 'c']
- The list is extended by adding three
'c'
characters.
Repeating a Sequence with loop
To repeat multiple values you must iterate over the list using nested loops:
a_list = [1, 2, 3]
new_list = []
count = 3
for _ in range(count): # for loop
for item in a_list: # nested for loop
new_list.append(item)
print(new_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Conclusion
Creating lists with repeated values is a common task.
- The multiplication operator (
*
) is concise for immutable objects, but be very careful with mutable objects like lists and dictionaries; use list comprehensions in that case. itertools.repeat()
provides an iterator-based approach, and loops offer flexibility when you need more control.
Understanding the distinction between repeating references and creating new objects is key to avoiding unexpected behavior.