How to declare an empty list in python – How to Create an Empty List in Python? – 2 Simple Methods

List:

How to declare an empty list in python: A list in Python is a collection of zero or more elements. A list element can be any type of data. It can be a string, a number, or a mixture of the two. In Python, a list is equivalent to an array in C or Java. Lists are mutable, which means you may change their content, and they contain a variety of helpful specialized methods.

A Python list is a data structure that efficiently performs the function of an Array. Furthermore, unlike arrays, a “list” keeps elements in a dynamic manner and can be used to store elements of different types.

As a result, Lists are a better substitute for the Array data structure because they may hold a variety of elements(heterogeneous elements).

Example

# Give the list as static input and store it in a variable.
gvn_lst = ['Hello', 'btechgeeks', 1, 8, 6]
# Print the given list
print(gvn_lst)

Output:

['Hello', 'btechgeeks', 1, 8, 6]

A List can be created using the below ways:

  • Using Square Brackets [ ]
  • Using the list() Function

1)Using Square Brackets [ ]

In Python, you would create a list by simply putting the sequence inside the square brackets[]. Simply assign a variable using square brackets to declare an empty list.

Syntax:

list = [ ]

Example1

# Create an empty list using the square brackets
gvn_lst = []
# Print the given empty list
print("The given empty list = ", gvn_lst)

Output:

The given empty list =  []

Example2

# Give the list as static input and store it in a variable.
gvn_lst = ['Hello', 'this', 'is', 'btechgeeks']
# Print the given list
print(gvn_lst)

Output:

['Hello', 'this', 'is', 'btechgeeks']

2)Using the list() Function

The Python list() function can be used to construct or create an empty list.

Syntax:

gvn_list = list(iterable)

Parameters

iterable: This is optional. It may be any sequence like string, tuple, dictionary, set, or iterator object.

Return Values:

  • If no arguments are given, an empty list is returned.
  • If a parameter is specified, it returns a list of iterable elements.

Example1

# Create an empty list using the list() function
gvn_lst = list()
# Print the given empty list
print("The given empty list = ", gvn_lst)

Output:

The given empty list =  []

Using the built-in list() function, we created an empty list. The list() method provides an empty list as output because no arguments were given to it.

Example2

# Create an empty list using the list() function
gvn_lst = list()
# Print the given empty list
print("The given empty list = ", gvn_lst)
# Print the type of given list using the type() function by passing the given list
# an an argument to it
print("The Type of the above given variable  = ", type(gvn_lst))
# Get the length of the  of given list using the len() function and print it
print("The length of the  of given list = ", len(gvn_lst))

Output:

The given empty list =  []
The Type of the above given variable  =  <class 'list'>
The length of the  of given list =  0