Open In App

How to add Elements to a List in Python

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are different methods used to add elements to a list in Python. There are 3 cases of adding an element to a list:

  1. Add element to list beginning
  2. Add element to list at index
  3. 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




#creating a list
my_list = [1, 2, 3]
#using append() to add elements
my_list.append(4)
#printing new list
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:



Previous Article
Next Article

Similar Reads

Python | Add list elements with a multi-list based on index
Given two lists, one is a simple list and second is a multi-list, the task is to add both lists based on index. Example: Input: List = [1, 2, 3, 4, 5, 6] List of list = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output: [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]] Explanation: [1] = [1+0] [2, 3, 4] = [0+2, 1+2, 2+2] [3, 4] = [3+0, 3+1] [4
5 min read
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be done. Input : test_list = [(5, 6), (2, 4), (5, 7), (2
6 min read
Python | Add list at beginning of list
Sometimes, while working with Python list, we have a problem in which we need to add a complete list to another. The rear end addition to list has been discussed before. But sometimes, we need to perform an append at beginning of list. Let's discuss certain ways in which this task can be performed. Method #1 : Using "+" operator The "+" operator ca
5 min read
Python - Replace index elements with elements in Other List
Sometimes, while working with Python data, we can have a problem in which we have two lists and we need to replace positions in one list with the actual elements from other list. Lets discuss certain ways in which this task can be performed. Method #1 : Using list comprehension This is one way to solve this problem. In this we just iterate through
6 min read
Python - Check if elements index are equal for list elements
Given two lists and check list, test if for each element in check list, elements occur in similar index in 2 lists. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7] Output : False Explanation : 7 is at 4th and 2nd place in both list, hence False. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 6, 9,
4 min read
Python - Group first elements by second elements in Tuple list
Given List of tuples, group 1st elements on basis of 2nd elements. Input : test_list = [(6, 5), (2, 7), (2, 5), (8, 7), (3, 7)] Output : {5: [6, 2], 7: [2, 8, 3]} Explanation : 5 occurs along with 6 and 2 in tuple list, hence grouping. Input : test_list = [(6, 5), (2, 7), (2, 5), (8, 7)] Output : {5: [6, 2], 7: [2, 8]} Explanation : 5 occurs along
5 min read
Python Program that prints elements common at specified index of list elements
Given a list of strings, the task is to write a Python program to extract all characters that are same at a specified index of each element of a list. Illustration: Input : test_list = ["geeks", "weak", "beak", "peek"] Output : ['e', 'k'] Explanation : e and k are at same at an index on all strings.Input : test_list = ["geeks", "weak", "beak", "pee
5 min read
Python program to print elements which are multiples of elements given in a list
Given a list, the task here is to write a Python program to extract elements which are multiple of all elements of custom list. Input : test_list = [4, 24, 8, 10, 12, 23], div_list = [6, 4] Output : [24, 12] Explanation : 24 and 12 divides 6 and 4 both. Input : test_list = [4, 24, 8, 10, 12, 23], div_list = [6, 4, 7] Output : [] Explanation : No el
5 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Python | Replace elements in second list with index of same element in first list
Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. Method #1: Using Iteration C/C++ Code # Python code to replace every element # in second list with index of first element. # List Initialization Input1 = ['cut', 'god', 'pass']
5 min read
Practice Tags :