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
# 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
OutputOriginal 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
#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
OutputThe 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.
Similar Reads
Python - Move Element to End of the List
We are given a list we need to move particular element to end to the list. For example, we are given a list a=[1,2,3,4,5] we need to move element 3 at the end of the list so that output list becomes [1, 2, 4, 5, 3].Using remove() and append()We can remove the element from its current position and th
3 min read
Python program to insert an element into sorted list
Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 min read
Python - Random insertion of elements K times
Given 2 list, insert random elements from List 2 to List 1, K times at random position. Input : test_list = [5, 7, 4, 2, 8, 1], add_list = ["Gfg", "Best", "CS"], K = 2 Output : [5, 7, 4, 2, 8, 1, 'Best', 'Gfg'] Explanation : Random elements from List 2 are added 2 times. Input : test_list = [5, 7, 4
5 min read
Move One List Element to Another List - Python
The task of moving one list element to another in Python involves locating a specific element in the source list, removing it, and inserting it into the target list at a desired position. For example, if a = [4, 5, 6, 7, 3, 8] and b = [7, 6, 3, 8, 10, 12], moving 10 from b to index 4 in a results in
3 min read
Get the Last Element of List in Python
In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list:Input: list = [1, 3, 34, 12, 6]Output: 6Explanation: Last element of the list l in the above example is 6.Let's explore various methods of doing it in Python:1. Using Neg
2 min read
How to Get the Number of Elements in a Python List
In Python, lists are one of the most commonly used data structures to store an ordered collection of items.In this article, we'll learn how to find the number of elements in a given list with different methods.ExampleInput: [1, 2, 3.5, geeks, for, geeks, -11]Output: 7Let's explore various ways to do
3 min read
Python - Element Index in Range Tuples
Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
7 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python - Append given number with every element of the list
In Python, lists are flexible data structures that allow us to store multiple values in a single variable. Often, we may encounter situations where we need to modify each element of a list by appending a given number to it. Given a list and a number, write a Python program to append the number with
5 min read
Shift Last Element to First Position in list - Python
The task of shifting the last element to the first position in a list in Python involves modifying the order of elements such that the last item of the list is moved to the beginning while the rest of the list remains intact. For example, given a list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to sh
3 min read