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

in #code3 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

Coin Marketplace

STEEM 0.23
TRX 0.12
JST 0.029
BTC 66946.15
ETH 3488.47
USDT 1.00
SBD 3.22