How to Add to a List in Python

MacBook Pro

To add an element 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 provides you a short answer. But let’s take a closer look at these and some other common ways for adding items to a list in Python.

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

1. Append() Method—Add an Element to the End of a 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.

For example:

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 (as shown later in this article).

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

To add an element at a specific place 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

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 similar 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]

4. List Merging with the + Operator in Python

To merge two or more lists together, you can use the + operator.

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]

Conclusion

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

  1. Add 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 + operator.

Thanks for reading. I hope you enjoyed it.

Happy coding!

Further Reading

Learn all the basics of Python lists

Learn how to map lists in Python

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *