Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Create a Python List of Size n?
1
How to Create a Python List of Size n?

To create a list of
Code:
n
placeholder elements, multiply the list of a single placeholder element with
Code:
n
. For example, use
Code:
[None] * 5
to create a list
Code:
[None, None, None, None, None]
with five elements
Code:
None
. You can then overwrite some elements with index assignments. In the example,
Code:
lst[2] = 42
would result in the changed list
Code:
[None, None, 42, None, None]
.



Let’s play with an interactive code shell before you’ll dive into the detailed solution!

https://trinket.io/embed/python/8b63f214b1

Exercise: Initialize the list with
Code:
n=20
placeholder elements
Code:
-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
Code:
n
. How to initialize a list with
Code:
n
placeholder elements?

Code:
# n=0 --> [] # n=1 --> [None] # n=5 --> [None, None, None, None, None]

Solution: Use the list concatenation operation
Code:
*
.

Code:
n = 5 lst = [None] * n print(lst) # [None, None, None, None, None]

You can modify the element
Code:
n
as you like. In subsequent operations, you can overwrite all placeholder
Code:
None
list elements using simple index assignment operations:

Code:
lst[0] = 'Alice' lst[1] = 0 lst[2] = 42 lst[3] = 12 lst[4] = '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 alist of lists):

Code:
lst = [[]] * n print(lst) # [[], [], [], [], []] lst[2].append(42) print(lst) # [[42], [42], [42], [42], [42]]

Changing one list element changes all list elements because all list elements refer to the same list object in memory:

https://pythontutor.com/iframe-embed.htm...nces=false

The solution is to use list comprehension (see my detailed blog tutorial on list comprehension for a complete guide):

Code:
lst = [[] for _ in range(n)] print(lst) # [[], [], [], [], []] lst[2].append(42) print(lst) # [[], [], [42], [], []]

In the following visualization, you can see how each element now refers to an independent list object in memory:

https://pythontutor.com/iframe-embed.htm...nces=false

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.

Join the free webinar now!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: