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.16
TRX 0.13
JST 0.027
BTC 58306.22
ETH 2596.07
USDT 1.00
SBD 2.39