In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
a = []
# Append an element to the list
a.append(10)
print(a)
Output
[10]
Other methods that we can use to append elements to a list are:
Table of Content
Using + Operator to Concatenate Lists
Another way to add elements to an empty list is by using the + operator. We can concatenate the original list with another list containing the element we want to add.
# Create an empty list
a = []
# Concatenate the list with a new list containing the element
a = a + [20]
print(a)
Output
[20]
Using extend() Method
The extend() method allows us to add multiple elements to a list. We can use it if we want to add more than one item at a time.
a = []
# Extend the list by adding multiple elements
a.extend([30, 40, 50])
print(a)
Output
[30, 40, 50]
Using List Unpacking
List unpacking is a more advanced method but it’s still a great option to add elements to a list.
a = []
#Use list unpacking to add an element
a = [*a, 60]
print(a)
Output
[60]