# How does repetition operator work on list in Python?

PythonServer Side ProgrammingProgramming

We are accustomed to using the * symbol to represent multiplication, but when the operand on the left side of the * is a list, it becomes the repetition operator. The repetition operator makes multiple copies of a list and joins them all together. Lists can be created using the repetition operator, *. For example,

## Example

numbers = [0] * 5
print numbers

## Output

This will give the output −

[0, 0, 0, 0, 0]

[0] is a list with one element, 0.  The repetition operator makes 5 copies of this list and joins them all together into a single list. Another example using multiple elements in the list.

## Example

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

## Output

This will give the output −

[0, 1, 2, 0, 1, 2, 0, 1, 2]

Note that Python creates shallow copies of the lists in this. So changing objects at one place will change them at all places they are repeated. If you don't want this behaviour, don't use repetition operator to create lists.

Updated on 12-Jun-2020 06:19:17