How does repetition operator work on list in Python?

In Python, the * symbol serves as the repetition operator when used with lists. Instead of multiplication, it creates multiple copies of a list and concatenates them together into a single new list.

Basic Syntax

The repetition operator follows this pattern ?

# Syntax: list * number
new_list = original_list * repetition_count

Single Element Repetition

Create a list with repeated single elements ?

numbers = [0] * 5
print(numbers)
[0, 0, 0, 0, 0]

The list [0] contains one element. The repetition operator makes 5 copies and joins them into a single list.

Multiple Element Repetition

Repeat lists containing multiple elements ?

numbers = [0, 1, 2] * 3
print(numbers)
[0, 1, 2, 0, 1, 2, 0, 1, 2]

The entire sequence [0, 1, 2] is repeated 3 times in order.

Practical Examples

Creating Default Values

# Initialize list with default values
scores = [0] * 10
print("Scores:", scores)

# Create a list of empty strings
names = [""] * 5
print("Names:", names)
Scores: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Names: ['', '', '', '', '']

Pattern Creation

# Create repeating patterns
pattern = ["A", "B"] * 4
print("Pattern:", pattern)

# Mixed data types
mixed = [1, "hello", True] * 2
print("Mixed:", mixed)
Pattern: ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B']
Mixed: [1, 'hello', True, 1, 'hello', True]

Important: Shallow Copy Behavior

The repetition operator creates shallow copies. For mutable objects like lists, this means all copies reference the same object ?

# Be careful with mutable objects
matrix = [[0] * 3] * 2
print("Original:", matrix)

# Modifying one affects all copies
matrix[0][0] = 5
print("After change:", matrix)
Original: [[0, 0, 0], [0, 0, 0]]
After change: [[5, 0, 0], [5, 0, 0]]

To avoid this behavior with nested lists, use list comprehension instead ?

# Correct way for nested lists
matrix = [[0] * 3 for _ in range(2)]
print("Original:", matrix)

matrix[0][0] = 5
print("After change:", matrix)
Original: [[0, 0, 0], [0, 0, 0]]
After change: [[5, 0, 0], [0, 0, 0]]

Conclusion

The repetition operator * is useful for creating lists with repeated elements or patterns. Remember that it creates shallow copies, so use list comprehension when working with mutable objects to avoid unintended side effects.

Updated on: 2026-03-24T19:05:01+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements