Skip to content

Python empty list of size n | Example code

  • by

You can create an empty list using the None Multiplication or Using the range() function in Python.

The multiplication method is best if you want to initialize a list with the same values. A range() a statement is a good choice if you want a list to contain values in a particular range.

list1 = [None] * size

OR

List1 = [None for x in range(size)]


List2 = [[] for x in range(size)]

Creating an empty list in Python example

Simple Python example codes:

Initialize List of Size N Using Multiplication

lst = [None] * 5
print(lst)

Output:

Python empty list of size n

Initialize List of Size N Using range()

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

Output: [[ ], [ ], [ ], [ ], [ ]]

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

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading