Python empty list of size n | Example code

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.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.