Python
Programming
Strings
Lists
Code Tutorial

Appending the same string to a list of strings in Python

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

Appending the same string to a Python list is simple, but the best expression depends on whether you want to mutate an existing list or create a new one. The good news is that strings are immutable, which makes some shorthand patterns completely safe here even though the same patterns would be dangerous for mutable inner objects.

Append Repeated Strings to an Existing List

If you already have a list and want to add the same string several times, use extend with list multiplication:

python
items = ["start"]
items.extend(["done"] * 3)
print(items)

Output:

text
['start', 'done', 'done', 'done']

This is usually clearer than writing a manual loop when every appended value is identical.

Build a New Repeated List

If you do not need to preserve an existing list object, create the repeated list directly:

python
retries = ["retry"] * 5
print(retries)

This is the simplest form when the list is just repeated string content.

Use a Loop Only When the Logic Changes Per Item

A loop is still appropriate when each element depends on iteration state:

python
1messages = []
2for i in range(5):
3    label = "ok" if i % 2 == 0 else "retry"
4    messages.append(label)
5
6print(messages)

If every appended value is exactly the same, the loop usually adds noise rather than clarity.

Why List Multiplication Is Safe for Strings

Python list multiplication has a bad reputation because it can duplicate references to mutable inner objects. That warning is real, but strings are immutable, so it does not apply here.

Safe with strings:

python
values = ["x"] * 3
values[0] = "y"
print(values)

Problematic with inner lists:

python
bad = [[]] * 3
bad[0].append("x")
print(bad)

In the second example, all three positions reference the same inner list. With strings, reassigning one element does not mutate a shared string object, so the pattern is safe.

append vs extend

This distinction matters:

python
items = ["a"]
items.append("b")
print(items)

Output:

text
['a', 'b']

Now compare extend:

python
items = ["a"]
items.extend(["b", "b", "b"])
print(items)

Output:

text
['a', 'b', 'b', 'b']

Use append when adding one element. Use extend when adding several elements from an iterable.

A Small Helper Function

If the pattern appears often, wrapping it can make the intent explicit:

python
1def append_repeated(base: list[str], text: str, count: int) -> list[str]:
2    if count < 0:
3        raise ValueError("count must be non-negative")
4
5    result = list(base)
6    result.extend([text] * count)
7    return result
8
9original = ["a", "b"]
10updated = append_repeated(original, "z", 4)
11print(original)
12print(updated)

This version returns a new list instead of mutating the caller's list, which can be useful in codebases that avoid side effects.

When You Do Not Need a List at All

Sometimes the real need is not to store repeated strings, but to iterate over them once. In that case, a generator can avoid building a large list:

python
1def iter_repeated(text: str, count: int):
2    for _ in range(count):
3        yield text
4
5for item in iter_repeated("event", 3):
6    print(item)

That is not an append operation anymore, but it is useful when the repeated values are only streamed.

Common Pitfalls

  • Writing a manual loop for a repeated constant value when extend([text] * n) already expresses the intent clearly.
  • Confusing append and extend and accidentally adding a whole list as one element.
  • Avoiding list multiplication because of mutable-object warnings even though strings are safe here.
  • Forgetting to validate counts in helper functions, which can hide bad input.
  • Building a huge repeated list when a generator would be enough for one-pass consumption.

Summary

  • Use extend([text] * n) to append the same string to an existing list.
  • Use [text] * n when creating a new repeated list.
  • Strings are immutable, so list multiplication is safe for this case.
  • Use loops only when per-item logic differs.
  • Consider generators when you need repeated values without storing a large list in memory.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms