Python | Return new list on element insertion
Last Updated :
02 May, 2023
The usual append method adds the new element in the original sequence and does not return any value. But sometimes we require to have a new list each time we add a new element to the list. This kind of problem is common in web development. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using + operator This task can be performed if we make a single element list and concatenate original list with this newly made single element list.
Python3
test_list = [ 5 , 6 , 2 , 3 , 9 ]
print ("The original list is : " + str (test_list))
K = 10
res = test_list + [K]
print ("The newly returned added list : " + str (res))
|
Output :
The original list is : [5, 6, 2, 3, 9]
The newly returned added list : [5, 6, 2, 3, 9, 10]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using * operator Similar kind of task can be used using * operator in which we use * operator to take all the elements and also add the new element to output the new list.
Python3
test_list = [ 5 , 6 , 2 , 3 , 9 ]
print ("The original list is : " + str (test_list))
K = 10
res = [ * test_list, K]
print ("The newly returned added list : " + str (res))
|
Output :
The original list is : [5, 6, 2, 3, 9]
The newly returned added list : [5, 6, 2, 3, 9, 10]
Method #3 : Here is another approach using the copy module from the copy library:
Python3
import copy
test_list = [ 5 , 6 , 2 , 3 , 9 ]
K = 10
res = copy.copy(test_list)
res.append(K)
print ( "Original list:" , test_list)
print ( "New list:" , res)
|
Output
Original list: [5, 6, 2, 3, 9]
New list: [5, 6, 2, 3, 9, 10]
This approach creates a new list and copies all the elements from the original list into the new list. Then, it appends the new element to the new list without modifying the original list.
Time complexity: O(n) (to copy all elements of the list)
Auxiliary space: O(n) (to store the copy of the original list)
Approach#4: Using insert()
This approach inserts the integer value 10 at index 5 of the list “lst” using the insert() method, and then prints the modified list. The insert() method takes two arguments – the index at which to insert the new element, and the element to insert.
Algorithm
1. Create the original list
2. Use the insert() method to add the new element at a specific position in the list
3. Print the resulting list
Python3
lst = [ 5 , 6 , 2 , 3 , 9 ]
lst.insert( 5 , 10 )
print (lst)
|
Output
[5, 6, 2, 3, 9, 10]
Time Complexity: O(n), where n is the length of the original list, as inserting an element at a specific position requires shifting all the elements after that position.
Space Complexity: O(1), as no new list is created.
Approach#5: Using reduce():
Algorithm:
- Import the functools module.
- Initialize the list and print the original list.
- Take an element to add to the list.
- Use the reduce method and a lambda function to add the element to the list.
- Print the result.
Python3
import functools
test_list = [ 5 , 6 , 2 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 10
res = functools. reduce ( lambda x, y: x + [y], [K], test_list)
print ( "The newly returned added list : " + str (res))
|
Output
The original list is : [5, 6, 2, 3, 9]
The newly returned added list : [5, 6, 2, 3, 9, 10]
Time Complexity: O(n), where n is the length of the original list. The reduce method iterates through the original list once.
Auxiliary Space: O(n+1), where n is the length of the original list. A new list of length n+1 is created to store the added element.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...