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)
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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)
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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'])
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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 listappend()
– 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✌✌