How to Create an Empty List in Python? | Python Examples Series

in code •  3 years ago  (edited)

In this tutorial, we will learn how to create an empty list. In other words, a List with no elements But before that What’s A Python List Exactly?

In short, a list is a collection of arbitrary objects, somewhat akin to an array in many other programming languages but more flexible. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ([])

image.png

To create an empty list in python, assign the variable with empty square brackets.
mylist = []

You can also use list class constructor as shown below to create an empty list.
mylist = list()

Example 1: Create an Empty List

In the following example, we will create an empty list and check the datatype of the variable.
books = []
print(type(books))
Output: <class 'list'>

Example 2: Create an Empty List

In the following example, we will create an empty list using list class constructor.
books = list()
print(type(books))
Output: <class 'list'>


images.jfif

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!