Friday, December 27, 2024
Google search engine
HomeLanguagesAdd Elements to a List in Python

Add Elements to a List in Python

Adding elements to a list is an important topic in Python programming which helps in the dynamic storage and manipulation of data. By adding elements, we can collect different data types into a single structure and use them for efficient data processing.

Add Items to a list in Python

  • Add an element to the front of the List in Python
  • Add an element to the end of the list.
  • Add an element in the middle of the list.

Add Element to Front of List in Python

We can add elements to the start of the Python list by using the following methods:

Using insert() Method

The insert() method allows you to add an element at a specific index in the list. To add an element to the start, you can use the index 0.

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.

Python Concatenation of List

You can create a new list containing the element you want to add and then concatenate it with the existing list.

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.

For methods pls refer this article.

Insert an Element at the End of a List

We can add elements to the end of the Python list by using the following methods.

Python append() Method

The append() method is used to add an element to the end of the 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.

Python extend() Method

The extend() method adds elements from another 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.

Python Concatenation List

You can concatenate two lists using the + operator to add elements to the end.

Python3




list1 = [1, 2, 3]
list2 = [4, 5]
combined_list = list1 + list2 
print(combined_list)


Output:

[1, 2, 3, 4, 5]

Time Complexity: O(n) – The + operation involves creating a new list by copying elements from both lists.
Space Complexity: O(n) – A new list is created to store the concatenated result.

Using List Unpacking

You can use unpacking to add elements from another iterable to the end.

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.

Python insert() Method

The insert() method can also be used to add an element to the end by specifying the last index.

Python3




my_list = [1, 2, 3]
my_list.insert(len(my_list), 4)
print(my_list)


Output:

[1, 2, 3, 4]

Time Complexity: O(n) – In the worst case, when inserting at the end, the elements after the insertion point need to be shifted.
Space Complexity: O(0) – No additional space is required beyond the original list.

Adding Items in the Middle of a list in Python

We can add elements to the end of the Python list by using the following methods.

Python insert() Method

The insert() method allows you to add an element at a specified index in the list. 

Python3




my_list = [1, 2, 4, 5]
my_list.insert(2, 3)
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) The insert() method needs to shift elements after the insertion index to accommodate the new element, which takes linear time in the worst case.
Space Complexity: O(1) The insert() method modifies the existing list in place and doesn’t require additional space proportional to the input size.

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 List Unpacking

You can use unpacking to add elements from another iterable at a specific index.

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.

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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments