How to add Elements to a List in Python
Last Updated :
27 Dec, 2023
There are different methods used to add elements to a list in Python. There are 3 cases of adding an element to a list:
- Add element to list beginning
- Add element to list at index
- Add element to list at last index
In this article, we will see how to add elements in list in Python.
How to Add Items to a List in Python?
Manipulating data structures in Python often requires adding elements to lists. There are various methods to accomplish this, each with its specific use case. Let’s see how to add elements to list in Python.
- insert() method
- append() method
- concatenation method
- extend() method
- list unpacking method
- Slice and concatenation method
Insert Element to List Using insert() Method
The insert()
method allows you to add elements to list at index. insert() method allows us to insert elements at a specific index.
Python3
my_list = [ 2 , 3 , 4 ]
my_list.insert( 0 , 1 )
print (my_list)
|
Output
[1, 2, 3, 4]
Time Complexity: O(n), where n is the number of elements in the list. This is because the insert method has to shift all the elements of the list to accommodate the newly inserted element, which takes O(n) time.
Auxiliary Space: O(1), as the insert method does not require any additional memory to be allocated.
Add Element to List using append() Method
append() method add element to the end of list. append() method is used to add item to a list at last index. You can pass multiple values to append multiple item to list.
Python3
my_list = [ 1 , 2 , 3 ]
my_list.append( 4 )
print (my_list)
|
Output
[1, 2, 3, 4]
Time Complexity: O(1) – average time to append an element remains constant.
Space Complexity: O(0) – No additional space is required.
Add Element to a List Using Concatenation
You can create a new list containing the element you want to add and then concatenate it with the existing list. You can concatenate two lists using the +
operator to add list to list in Python.
Python3
my_list = [ 2 , 3 , 4 ]
new_element = [ 1 ]
my_list = new_element + my_list
print (my_list)
|
Output
[1, 2, 3, 4]
Time complexity: O(n)
Auxiliary space: O(n) where n is the length of the list.
Add Element to a List Using extend()
Method
The extend()
method adds elements from another list(or any iterable) to the end of the list.
Python3
my_list = [ 1 , 2 , 3 ]
new_elements = [ 4 , 5 ]
my_list.extend(new_elements)
print (my_list)
|
Output
[1, 2, 3, 4, 5]
Time Complexity: O(k), where k is the number of elements in the iterable being extended.
Space Complexity: O(k) – Space is required to store the elements being added.
Add Element to a List using List Unpacking
You can use unpacking to add elements from another list(or any iterable) to the end of list or at specific index.
Example 1. Add Elements at the end of list
Python3
my_list = [ 'a' , 'b' , 'c' ]
new_elements = [ 4 , 5 ]
my_list + = new_elements
print (my_list)
|
Output
['a', 'b', 'c', 4, 5]
Time Complexity: O(n) – Similar to the +
operator, as it involves copying elements to a new list.
Space Complexity: O(n) – A new list is created to store the unpacked elements.
Example 2. Add Elements at specific index in a list
Python3
my_list = [ 'a' , 'b' , 'd' , 'e' ]
new_elements = 'c'
index = 2
my_list = [ * my_list[:index], * new_elements, * my_list[index:]]
print ( "New list after inserting 'c' at index 2 is" )
print (my_list)
|
Output
New list after inserting 'c' at index 2 is
['a', 'b', 'c', 'd', 'e']
Time Complexity: O(n) Unpacking involves creating a new list with the elements before and after the index, along with the new element. This requires iterating over the original list, resulting in linear time.
Space Complexity: O(n) Creating a new list to hold the unpacked elements increases the space complexity linearly with the input size.
Add Element to a List Using Slicing and Concatenation
You can slice the list into two parts, then concatenate the new element and the second part of the original list.
Python3
my_list = [ 1 , 2 , 4 , 5 ]
new_element = 3
index = 2
my_list = my_list[:index] + [new_element] + my_list[index:]
print ( "New list after inserting 3 at index 2 is" )
print (my_list)
|
Output
New list after inserting 3 at index 2 is
[1, 2, 3, 4, 5]
Time Complexity: O(n) Slicing and concatenation involve creating a new list with the existing and new elements. This requires iterating over the original list and copying its elements, which takes linear time.
Space Complexity: O(n) Creating a new list to hold both parts (before the index and after the index) increases the space complexity linearly with the input size.
Python Slice Assignment
We can use the slice method to add the elements in the middle of the list
Python3
my_list = [ 'a' , 'b' , 'c' , 'e' ]
new_element = 'd'
index = 3
my_list[index:index] = new_element
print ( "New list after inserting 'd' at index 3 is" )
print (my_list)
|
Output
New list after inserting 'd' at index 3 is
['a', 'b', 'c', 'd', 'e']
Time Complexity: O(n) Creating the new list with the elements to add and using the extend()
method to insert it into the original list both take linear time.
Space Complexity: O(n) Creating a new list to hold the elements to be added increases the space complexity linearly with the input size.
In this article we have covered different methods to add elements in a list. You can add element at index 0, at end or a specific index using one of these above methods.
Read More Operations on List:
Please Login to comment...