How to Add Elements to an Empty List in Python?

In this tutorial, I will explain how to add elements to an empty list in Python. Recently in a Python webinar, someone asked me question about adding elements to an empty list, which made me research more about this topic. After testing and experimenting with various methods, I found three effective ways to achieve this task. Let us learn more about this topic.

Create an Empty List

To begin, let’s create an empty list in Python. There are two ways to achieve this:

  1. Use square brackets []:
   my_list = []
  1. Use the list() constructor:
   my_list = list()

Both approaches result in an empty list assigned to the variable my_list. You can create an empty list using either square brackets or the list() constructor.

Read How to Remove None Values from a List in Python?

Add Elements to an Empty List in Python

Now that we have an empty list, let’s explore different methods to add elements to it.

Approach 1: Use the append() Method

The append() method in Python is the most common way to add elements to the end of a list. You can add elements to an empty list using the append() method. Here’s an example:

# Create an empty list
cities = []

# Add elements to the list using append()
cities.append("New York")
cities.append("Los Angeles")
cities.append("Chicago")

print(cities)

Output:

['New York', 'Los Angeles', 'Chicago']

You can see the output in the below screenshot.

Add Elements to an Empty List in Python

In this example, we start with an empty list called cities. We then use the append() method to add city names to the list one by one. The append() method adds the element to the end of the list.

Check out How to Add Tuples to Lists in Python?

Approach 2: Use the insert() Method

If you want to add an element at a specific position in the list, you can use the insert() method. Python insert() method adds the element at a specified index. Here’s an example:

# Create an empty list
presidents = []

# Add elements to the list using insert()
presidents.insert(0, "George Washington")
presidents.insert(1, "John Adams")
presidents.insert(1, "Thomas Jefferson")

print(presidents)

Output:

['George Washington', 'Thomas Jefferson', 'John Adams']

You can see the output in the below screenshot.

How to Add Elements to an Empty List in Python

In this example, we create an empty list called presidents. We use the insert() method to add presidents to the list at specific positions. The insert() method takes two arguments: the index where the element should be inserted and the element itself.

Check out How to Select Items from a List in Python?

Approach 3: Use the extend() Method

If you have another list and want to add all its elements to the empty list, you can use the extend() method. You can append to a list that is empty using the extend() method in Python. Here’s an example:

# Create an empty list
states = []

# Create another list with states
more_states = ["California", "Texas", "Florida"]

# Add elements from more_states to states using extend()
states.extend(more_states)

print(states)

Output:

['California', 'Texas', 'Florida']

You can see the output in the below screenshot.

Add Elements to an Empty List in Python extend() Method

In this example, we have an empty list called states and another list called more_states containing state names. We use the extend() method to add all the elements from more_states to the states list.

Check out How to Write a List to a File in Python?

Add Elements to a 2D List

You can also create an empty Python 2D list (a list of lists) and add elements to it. To create an empty 2D list in Python, you can use nested square brackets or the list() constructor. Here’s an example:

# Create an empty 2D list
matrix = []

# Add rows to the matrix
matrix.append([1, 2, 3])
matrix.append([4, 5, 6])
matrix.append([7, 8, 9])

print(matrix)

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In this example, we create an empty 2D list called matrix. We then use the append() method to add rows (sublists) to the matrix. Each row is itself a list containing elements.

Read How to Iterate Through a List in Python?

Conclusion

In this tutorial, I have explained how to add elements to an empty list in Python. I discussed three approaches such as using the append() method, using the insert() method, and using the extend() method. I also covered how to add elements to a 2D list.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.