4 Ways to Add Elements to a List in Python (Theory & Examples)

To add elements to a list in Python, use the append() method:

list.append(item)

For example:

nums = [1, 2, 3]
nums.append(4)

print(nums)

Output:

[1, 2, 3, 4]

To add multiple elements to a list, use the built-in extend() method:

list.extend(items)

For example:

nums = [1, 2, 3]
nums.extend([4, 5, 6])

print(nums)

Output:

[1, 2, 3, 4, 5, 6]

This is a comprehensive guide to adding to lists in Python. Besides the quick answer above, you’ll learn different methods of how to add values to a list.

Let’s jump into it!

Adding Elements to a List in Python

There are four main ways to add elements to a list in Python:

  1. append() method
  2. insert() method
  3. extend() method
  4. List concatenation using the + operator

Let’s take a closer look at how each method works.

1. Append() Method—Add an Element to the End of a List

Python list append method adds an element to the end of the list

A popular way to add an element to a list is using the built-in append() method. It takes a single element as its argument and adds it to the end of the list. This method is useful as most of the time you want to add elements to the end of the list — just like this method does.

For example, let’s add a fifth number to the end of a list of four numbers:

nums = [1, 2, 3, 4]
nums.append(5)

print(nums)

Output:

[1, 2, 3, 4, 5]

If you wish to add multiple items to the end of the list, use the extend() method. You’ll learn about this method in just a bit.

2. Insert() Method—Add an Element to a Specific Index of a List

Python list insert method adds a value to a specific index of the list

To add an element at a specific position in the list, use the Python list’s built-in insert() method.

This method takes an index and an element as its arguments. The index shows at which position you want to add the element to the list.

For example, let’s add the number 10 as the first element in a list of numbers:

nums = [1,2,3]
nums.insert(0,10)

print(nums)

Output:

[10, 1, 2, 3]

3. Extend() Method—Add Multiple Items to a List

Python list extends adds a list of values to the end of a list

Sometimes you need to add multiple values to the end of a list in the same go.

In this case, you can use the built-in extend() method. It works similarly to the append() method, but it takes a list as an argument and adds each element to the end of the list.

Let’s see an example:

nums = [1,2,3]
nums.extend([10, 20, 30])

print(nums)

Output:

[1, 2, 3, 10, 20, 30]

Feel free to check a thorough comparison between the append() and extend() methods.

4. List Merging with the + Operator in Python

The + operator adds values to the end of a Python list

To merge two or more lists together, you can also use the + operator. This takes the list on the left-hand side and adds the element of the list on the right-hand side to it. Notice that this operator doesn’t directly modify either of the lists. Instead, it creates a new one.

For example:

nums1 = [1, 2, 3]
nums2 = [4, 5, 6]

result = nums1 + nums2
print(result)

Result:

[1, 2, 3, 4, 5, 6]

Another example of merging more than two lists:

nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
nums3 = [7, 8, 9]

result = nums1 + nums2 + nums3
print(result)

Output:

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

If you want to modify the original list when adding values with the + operator, use the += operator instead.

For example:

nums = [1,2,3]
more_nums = [4,5,6]

nums += more_nums

print(nums)

Output:

[1,2,3,4,5,6]

Conclusion

In Python, there are four ways to add elements to a list.

  1. Add a single element to the end with append() method.
  2. Add a single element to a specific index with insert() method.
  3. Add multiple elements to the end of the list using the extend() method
  4. Merge lists with the + operator.

Thanks for reading. I hope you enjoyed it.

Happy coding!

Further Reading