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

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.
Search for a command to run...

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.
No comments yet. Be the first to comment.
When you’re building an application, one thing you can’t skip is API testing. Whether it’s a login flow, payment gateway, or a complex e-commerce workflow, ensuring your APIs behave correctly saves you from nasty surprises in production. I had alread...

PyPy is an implementation of Python written in RPython (Reduced Python) language, and it is seen as a replacement for CPython. PyPy claims that it is almost a drop-in replacement for CPython and can beat it on speed and memory usage. It supports libr...

You must have used functions provided by the os module in Python several times in your projects. These could be used to create a file, walk down a directory, get info on the current directory, perform path operations, and more. In this article, we’ll...

FastAPI is a fast and modern web framework known for its support for asynchronous REST API and ease of use. In this article, we’ll see how to stream videos on frontend in FastAPI. StreamingResponse Stream Local Video FastAPI provides a StreamingRespo...

Have you ever come across circular imports in Python? Well, it’s a very common code smell that indicates something’s wrong with the design or structure. Circular Import Example How does circular import occur? This import error usually occurs when two...
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.
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.
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)
# 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.
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)
# 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']]
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'])
# 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.
| 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.
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.
âś…How Python reverse() and reversed() differ from each other.
âś…Different ways to remove whitespaces from the string.
âś…Different types of string formatting in Python.
âś…Build a Covid-19 EDA and Viz streamlit app in Python.
âś…What is *args and **kwargs in Python.
âś…Powerful one-liners in Python to boost the code.
That's all for now
Keep Coding✌✌