Skip to main content

How to Repeat a List in Python with *

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes you need a list that holds the same items several times: five zeros to start counting from, or one word repeated to build a test text. In Python, * between a list and a whole number repeats the list's items that many times. This guide repeats three lists on Python 3.15.2.

Syntax​

Syntax
<list> * <times>
PartWhat it means
<list>The list whose items are repeated
<times>A whole number: how many times the items appear in the new list. <times> * <list> works too

The Python documentation on sequence operations describes s * n or n * s as "equivalent to adding s to itself n times". The result is a new list; the original stays as it was.

The Example​

Three lists: two cheers repeated twice, a single zero repeated five times, and a laugh repeated three times and joined into one text.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

repeat.py
cheers = ["hip", "hip"] * 2
print(cheers)

zeros = [0] * 5
print(zeros)

print(" ".join(["ha"] * 3))

The repeat.py script in a macOS-style code window: the list hip, hip repeated twice, the list of one zero repeated five times, and the list of ha repeated three times and joined with spaces.

Step 2: Run It​

Terminal
uv run python repeat.py
Output
['hip', 'hip', 'hip', 'hip']
[0, 0, 0, 0, 0]
ha ha ha

The terminal after uv run python repeat.py: a list of four hip items, a list of five zeros, and the text ha ha ha.

  • ["hip", "hip"] * 2 repeated both items twice, in order: four items.
  • [0] * 5 made a list of five zeros from a list of one.
  • ["ha"] * 3 made a list of three "ha", and " ".join(...) put spaces between them.

The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Repeating a list that holds a list, such as [[]] * 3The documentation warns that "items in the sequence s are not copied; they are referenced multiple times", so all three are one list. Its example, adding 3 to the first of them, printed [[3], [3], [3]] when I ran itMake a new list for each, as the documentation's own fix does: [[] for i in range(3)]
Expecting * 2 to double every number in a list* repeats the items, as ["hip", "hip"] * 2 did; it doesn't multiply themMultiply each item, for example with a list comprehension

FAQ​

Does the order matter, list first or number first?​

No. 3 * [0] printed [0, 0, 0], the same as [0] * 3.

What happens with 0 or a negative number?​

You get an empty list: [0] * 0 and [0] * -2 both printed []. The documentation: "Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s)."

Conclusion​

["hip", "hip"] * 2 gave four items, [0] * 5 five zeros, and ["ha"] * 3 three laughs: * repeated each list's items in a new list.

Additional Resources​