# Difference Between insert(), append() And extend() In Python With Examples

**List** is one of Python's built-in data structures for storing collections of modifiable and ordered data. Lists can hold a variety of data types, including strings, integers, and even lists.

Lists are mutable, which means they can be created and modified. Python provides some methods for modifying the data within the list.

This article will explain the distinctions between the list ***insert()***, ***append()***, and ***extend()*** methods. We'll see how they differ and how they're used to modify the list.

## Methods Objective

Every Python developer would have worked with lists and used these methods periodically. List `insert()`, `append()`, and `extend()` are somewhat related because they are used to add elements to a list.

However, these methods are syntactically and programmatically distinct, each method has their own way of adding data to the list.

## List insert()

We already know what is the meaning of **insert**, it means putting or adding a particular element into the data. This method does the same work as its name meaning.

Python list `insert()` method helps to insert an element to the list at the desired position. This method takes two arguments, the first argument is the **index** at which the element is to be inserted and the second argument is the **element** that will be inserted.

`list.insert(index, element)`

```python
# List of names
friends = ['Sachin', 'Rishu', 'Yashwant']
# Printing the original list
print('Original List:', friends)
# Inserting an element
friends.insert(1, 'Abhishek')
# Printing the updated list
print('New List: ', friends)

----------
Original List: ['Sachin', 'Rishu', 'Yashwant']
New List:  ['Sachin', 'Abhishek', 'Rishu', 'Yashwant']
```

Using the `insert()` method, we inserted the string `'Abhishek'` at the **1st index** in `my_lst`.

## List append()

Python list `append()` method **adds the element to the end of the list**. It takes only one argument which is the element to be appended to the list.

`list.append(element)`

```python
# List of names
friends = ['Sachin', 'Rishu', 'Yashwant']
# Printing the original list
print('Original List:', friends)
# Appending an element
friends.append(['Abhishek', 'Yogesh'])
# Printing the updated list
print('New List: ', friends)

----------
Original List: ['Sachin', 'Rishu', 'Yashwant']
New List:  ['Sachin', 'Rishu', 'Yashwant', ['Abhishek', 'Yogesh']]
```

## List extend()

We've seen how we can use `append()` to add an element to the end of a list. List `extend()` does the same thing, but instead of adding a single element, it **adds each item from the iterable**, resulting in the list being extended.

`list.extend(iterable)` or `list.extend(['elem1', 'elem2', 'elem3'])`

```python
# List of names
friends = ['Sachin', 'Rishu', 'Yashwant']
# Printing the original list
print('Original List:', friends)
# Using extend() to extend the list
friends.extend(['Abhishek', 'Yogesh'])
# Printing the updated list
print('New List: ', friends)

----------
Original List: ['Sachin', 'Rishu', 'Yashwant']
New List:  ['Sachin', 'Rishu', 'Yashwant', 'Abhishek', 'Yogesh']
```

We passed the list in the expression `friends.extend(['Abhishek', 'Yogesh'])` and as we can see the items became part of the original list.

## Difference

| insert() | append() | extend() |
| --- | --- | --- |
| Used to add the element at the desired position in the list | Used to add the element at the end of the list | Used to add the elements from the iterable at the end of the list |
| `list.insert(index, element)` | `list.append(element)` | `list.extend(iterable)` |
| Takes two parameters | Takes a single parameter | Takes a single iterable parameter |
| Can take iterable but adds it as it is | Can take iterable but adds it as it is | Takes iterable and each item is added individually |
| List length increases by 1 | List length increases by 1 | Length of the list increases by the number of items in the iterable |
| Time complexity is constant i.e., O(1) | Time complexity is linear i.e., O(n) | Has time complexity of O(x), where **x** is the length of the iterable |

> Time complexity refers to the computer time to run an algorithm.

## Conclusion

We've seen how the list ***insert()***, ***append()***, and ***extend()*** methods differ from one another in this article. The goal of these methods is the same in that they add elements to the list, but they differ in how they add the elements to the list.

We've compared these methods using the code examples and here's a recap of what we've learned

* `insert()` - This method is used to insert the element at the desired position in the list
    
* `append()` - This method is used to add the element to the end of the list.
    
* `extend()` - This method is used to add each item from the iterable to the end of the list.
    

---

🏆**Other articles you might be interested in if you liked this one**

✅[8 different ways to reverse a Python list](https://geekpython.in/different-ways-to-reverse-a-python-list).

✅[How Python reverse() and reversed() differ from each other](https://geekpython.in/reverse-vs-reversed-in-python).

✅[Different ways to remove whitespaces from the string](https://geekpython.in/ways-to-remove-whitespaces-from-the-string-in-python-with-examples-beginners-guide).

✅[Different types of string formatting in Python](https://geekpython.in/4-ways-of-string-formatting-in-python-guide).

✅[Build a Covid-19 EDA and Viz streamlit app in Python](https://geekpython.in/python-web-app-under-100-lines-of-code-using-streamlit).

✅[What is \*args and \*\*kwargs in Python](https://geekpython.in/understanding-args-and-kwargs-in-python-best-practices-and-guide).

✅[Powerful one-liners in Python to boost the code](https://geekpython.in/one-liners-in-python).

---

**That's all for now**

**Keep Coding✌✌**
