Open In App

Python | Return new list on element insertion

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# returning new list on element insertion
# using + operator
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# element to add
K = 10
 
# using + operator
# returning new list on element insertion
res = test_list + [K]
 
# printing result
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




# Python3 code to demonstrate
# returning new list on element insertion
# using * operator
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# element to add
K = 10
 
# using * operator
# returning new list on element insertion
res = [*test_list, K]
 
# printing result
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 the copy module
import copy
# Initialize the list
test_list = [5, 6, 2, 3, 9]
# Element to add
K = 10
# Make a copy of the list using the copy module and Append the new element to the copied list
res = copy.copy(test_list)
res.append(K)
# Printing the original and new lists
print("Original list:", test_list)
print("New list:", res)
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Import the functools module.
  2. Initialize the list and print the original list.
  3. Take an element to add to the list.
  4. Use the reduce method and a lambda function to add the element to the list.
  5. Print the result.

Python3




import functools
 
#initializing list
test_list = [5, 6, 2, 3, 9]
 
#printing original list
print("The original list is : " + str(test_list))
 
#element to add
K = 10
 
#using reduce method
#returning new list on element insertion
res = functools.reduce(lambda x, y: x + [y], [K], test_list)
 
#printing result
print("The newly returned added list : " + str(res))
#This code is contributed by Vinay Pinjala


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
Previous
Next
Share your thoughts in the comments

Similar Reads