Python Tutorial

Adding Elements to Python List Using append(), insert() and extend(), Method

How to Add Elements to a List in Python?

In order to add elements to a Python list, there are three methods. We have explained these methods below with examples.

1. Using append() method in Python

You can use the append() method in Python to add an item to the end of the list:

Example:

thislist = ["pear", "melon", "orange"]

thislist.append("orange")

print(thislist)

Output:

['pear', 'melon', 'orange', 'orange']

2. Using insert() method in Python

Use the insert method in Python to insert a list item at a certain index. It inserts an item at the supplied index with the insert() function:

Example:

thislist = ["pear", "melon", "orange"]

thislist.insert(1, "orange")

print(thislist)

Output: 

['pear', 'orange', 'melon', 'orange']

3. Using extend() method in Python

Use the extend() function in Python to attach entries from another list to the current one.

Example:

thislist = ["pear", "melon", "orange"]

tropical = ["mango", "pinepear", "papaya"]

thislist.extend(tropical)

print(thislist)

Output:

['pear', 'melon', 'orange', 'mango', 'pinepear', 'papaya']
Did you find this article helpful?