Python: How to Create an Empty List and Append Items to it?

An ordered list of values is a collection. There may be different types of values. A list is a container that is mutable. This means that the existing ones can be added, deleted, or modified.

The list of Pythons represents a finite sequence mathematical concept. List values are called list items or list elements. A list may multiple times contain the same value. Each event is considered to be a separate element.

In this article, we will look at how to create an empty list and add elements to the list in Python.

How to Create An Empty List and How to Append a list of Items in Python?

There are several ways to create and append values to list some of them are:

Creating a list:

Adding elements to list:

Creating a list

Method #1: Using [] symbol create an empty list in python

An empty list can be generated in Python by simply writing square brackets, i.e. []. If there are no arguments in the square brackets [, it returns an empty list.

Using [] symbol

Implementation:

# creating new list
newlist = []
# printing it
print("Newlist = ", newlist)

Output:

Newlist =  []

Method #2: Using list() Constructor create an empty list in python

The list class in Python has a constructor

list( [iterable] ).

It takes an optional statement, which is an iterable sequence, and constructs a list from these elements. It returns an empty list if Sequence is not given. Let’s use this to make a blank list.

Using list() Constructor

Implementation:

# creating new list
newlist = list()
# printing it
print("Newlist = ", newlist)

Output:

Newlist =  []

Adding items/elements to the list

Method #1: Using append() function add items to an empty list in python

The built-in append() function can be used to add elements to the List. The append() method can only add one element to the list at a time; loops are used to add several elements to the list with the append() method. Since tuples are immutable, they can also be added to the list using the append method. Lists, unlike Sets, can be appended to an existing list using the append() form.

Using append() function

Below is the implementation:

# creating new empty list
newlist = list()
# adding items 1 2 3 to it
newlist.append(1)
newlist.append(2)
newlist.append(3)
# printing it
print("Newlist = ", newlist)

Output:

Newlist =  [1, 2, 3]

Method #2:Using insert() function add elements to an empty list in python

insert():

It inserts the item in the list at the given index.

list.insert(index,element)

The append() method only adds elements to the end of the List; the insert() method is used to add elements to the desired location. Unlike append(), which only needs one statement, insert() requires two (index,element).

creating new empty list Using insert() function

Below is the implementation:

# Taking a list with values
newlist = ['this', 'is', 'BTechGeeks']
# inserting element hello to 2 nd index using insert()
newlist.insert(2, 'hello')
# printing the list
print("list = ", newlist)

Output:

list =  ['this', 'is', 'hello', 'BTechGeeks']

Method #3:Using extend() function add items to an empty list in python

There is one more method to add elements other than the append() and insert() methods, extend(), used at the end of the list to add multiple elements simultaneously.

Note: the methods append() and extend() can only add elements at the end.

Below is the implementation:

# Taking a list with values
newlist = ['this', 'is', 'BTechGeeks']
# extend this newlist with multiple items
newlist.extend(['hello', 'python', 234])
# printing the list
print("list = ", newlist)

Output:

list =  ['this', 'is', 'BTechGeeks', 'hello', 'python', 234]

Related Programs: