Create Empty List with certain size in Python

In this article, we will discuss different ways to create an empty list with certain size.

Table Of Contents

Introduction

An empty list of size N means, that list will contain N None values. For example, an empty list of size 5 is,

emptyList = [None, None, None, None, None] 

There are different ways to create a list of given size and with None values only. Let’s discuss them one by one.

Create an empty list with certain size using astrik

First, create a list with one None value only, and then multiply that with N. Where, N is the size of empty list. It will give us a list with N None values. for example,

Advertisements
N = 5

# Create an empty List of size N
emptyList = [None] * N

print(emptyList)

Ouput:

[None, None, None, None, None]

It created a list of 5 None values.

Create an empty list with certain size using List Comprehension

Inside a List comprehension, iterate from 0 till N and add None to a list. Where, N is the size of empty list. It will give us a list with N None values. For example,

N = 5

# Create an empty List of size N
emptyList = [None for i in range(N) ]

print(emptyList)

Ouput:

[None, None, None, None, None]

It created a list of 5 None values.

Summary

We learned how to create an empty list of given size in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top