Python create empty list of size | Example code

Use [None] * 10 if want to create a list size of 10. Where each position is initialized to None. After that, you can add elements to it.

This code will create a list of size 10,

List = [None] * 10

Note: None is frequently used in Python to represent the absence of a value.

Another way to creates an empty list of sizes is by using list comprehensions:

[None for x in range(5)]

Create an empty list in Python with a certain size example

Python simple code Create an empty list with the given size.

Using None:

lst = [None] * 5
print(lst)

Output:

Python create empty list of size

Using list comprehensions:

lst = [None for x in range(5)]
print(lst)

Or

lst = [[] for i in range(5)]
print(lst)

Do comment if you have any doubts and suggestions on this Python list code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.