// Tutorial //

How to add Elements to a List in Python

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
How to add Elements to a List in Python

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

In this tutorial, we will learn different ways to add elements to a List in Python.

Methods to add elements to List in Python

There are four methods to add elements to a List in Python.

  1. append(): append the object to the end of the list.
  2. insert(): inserts the object before the given index.
  3. extend(): extends the list by appending elements from the iterable.
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.

Python add elements to List Examples

We can add an element to the end of the list or at any given index. There are ways to add elements from an iterable to the list. We can also use + operator to concatenate multiple lists to create a new list.

1. append()

This function add the element to the end of the list.

fruits = ["Apple", "Banana"]

# 1. append()
print(f'Current Fruits List {fruits}')

f = input("Please enter a fruit name:\n")
fruits.append(f)

print(f'Updated Fruits List {fruits}')

Output:

Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Orange
Updated Fruits List ['Apple', 'Banana', 'Orange']

2. insert()

This function adds an element at the given index of the list. It’s useful to add an element at the specified index of the list.

num_list = [1, 2, 3, 4, 5]

print(f'Current Numbers List {num_list}')

num = int(input("Please enter a number to add to list:\n"))

index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:\n'))

num_list.insert(index, num)

print(f'Updated Numbers List {num_list}')

Output:

Current Numbers List [1, 2, 3, 4, 5]
Please enter a number to add to list:
20
Please enter the index between 0 and 4 to add the number:
2
Updated Numbers List [1, 2, 20, 3, 4, 5]

3. extend()

This function append iterable elements to the list. It is useful to append elements from an iterable to the end of the list.

list_num = []
list_num.extend([1, 2])  # extending list elements
print(list_num)
list_num.extend((3, 4))  # extending tuple elements
print(list_num)
list_num.extend("ABC")  # extending string elements
print(list_num)

Output:

[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']

4. List Concatenation

If you have to concatenate multiple lists, you can use the “+” operator. This will create a new list and the original lists will remain unchanged.

evens = [2, 4, 6]
odds = [1, 3, 5]

nums = odds + evens
print(nums)  # [1, 3, 5, 2, 4, 6]

The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

Conclusion

It’s very easy to add elements to a List in Python programming. We can append an element at the end of the list, insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator. References:


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Pankaj

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

list1 = [“M”, “na”, “i”, “Ke”] list2 = [“y”, “me”, “s”, “lly”] list3 = [i + j for i,j in zip(list1, list2)] How is the above different to this; list3 = list() for i,j in zip(list1, list2): list3 = i + j how are these 2 codes different from one another?

- Asad Jaffer

    how to use add method like i mean list.add(x)… explain

    - Abdul

      new_list=elements[::2] #explain this

      - Varada

        is there anything like list.add(num)

        - harshita