To create a list of
n placeholder elements, multiply the list of a single placeholder element with
n. For example, use
[None] * 5 to create a list
[None, None, None, None, None] with five elements
None. You can then overwrite some elements with index assignments. In the example,
lst = 42 would result in the changed list
[None, None, 42, None, None].
Let’s play with an interactive code shell before you’ll dive into the detailed solution!
Exercise: Initialize the list with
n=20 placeholder elements
-1 and run the code.
Next, you’ll learn about the more formal problem and dive into the step-by-step solution.
Problem: Given an integer
n. How to initialize a list with
n placeholder elements?
# n=0 -->  # n=1 --> [None] # n=5 --> [None, None, None, None, None]
Solution: Use the list concatenation operation
n = 5 lst = [None] * n print(lst) # [None, None, None, None, None]
You can modify the element
n as you like. In subsequent operations, you can overwrite all placeholder
None list elements using simple index assignment operations:
lst = 'Alice' lst = 0 lst = 42 lst = 12 lst = 'hello' print(lst) # ['Alice', 0, 42, 12, 'hello']
However, there’s a small problem if you want to create a list with mutable objects (such as a list of lists):
lst = [] * n print(lst) # [, , , , ] lst.append(42) print(lst) # [, , , , ]
Changing one list element changes all list elements because all list elements refer to the same list object in memory:
The solution is to use list comprehension (see my detailed blog tutorial on list comprehension for a complete guide):
lst = [ for _ in range(n)] print(lst) # [, , , , ] lst.append(42) print(lst) # [, , , , ]
In the following visualization, you can see how each element now refers to an independent list object in memory:
Exercise: Run the visualization and convince yourself that only one element is modified! Why is this the case?
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.