Sometimes, while working with data, we have just a simple application in which we require to double the contents of a list and make it 100% increase in its magnitude. This is having applications in web development and machine learning domains. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop This the brute force way in which this task can be performed. In this, we just add the same element again to that index element and all the contents of list are added to itself i.e doubled.
Python3
# Python3 code to demonstrate # Double List # using loop # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ("The original list is : " + str (test_list)) # Double List # using loop res = [] for ele in test_list: res.append(ele + ele) # printing result print ("Double List is : " + str (res)) |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
Time complexity: O(n), where n is the length of the input list test_list. The loop iterates through the list and doubles each element, taking O(n) time.
Auxiliary space: O(n), as the code creates a new list res with the same number of elements as the input list.
Method #2 : Using list comprehension This task can also be performed using list comprehension. This is similar to above function. Just the difference is that its compact and one liner.
Python3
# Python3 code to demonstrate # Double List # using list comprehension # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ("The original list is : " + str (test_list)) # Double List # using list comprehension res = [ele + ele for ele in test_list] # printing result print ("Double List is : " + str (res)) |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
Time complexity: O(n), where n is the number of elements in the list test_list.
Auxiliary space: O(n), as a new list res is created to store the result. The size of the new list is the same as the size of the original list.
Method #3 : Using for loop
Python3
# Python3 code to demonstrate # Double List # using loop # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ( "The original list is : " + str (test_list)) # Double List # using loop res = [] for ele in test_list: res.append(ele * 2 ) # printing result print ( "Double List is : " + str (res)) |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
Time Complexity: O(n), where n is the number of elements in the list. The algorithm takes linear time as it is simply looping through all the elements of the list and performing a single operation on each element.
Auxiliary Space: O(n), where n is the number of elements in the list. The algorithm uses a separate list to store the result, which takes up additional space proportional to the size of the original list.
Method #4: Naive Approach Without Extra Space Complexity
Python3
# Python3 code to demonstrate # Double List # using loop # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ( "The original list is : " + str (test_list)) # Double List # using loop for i in range ( len (test_list)): test_list[i] = test_list[i] + test_list[i] # printing result print ( "Double List is : " + str (test_list)) |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #5: Using map() function
The map() function applies a given function to each item of an iterable (e.g., list) and returns a map object that can be converted to a list. We can use the map() function to create a new list by applying a function (in this case, adding an element to itself) to each element of the original list.
Python3
# Python3 code to demonstrate # Double List # using map() function # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ( "The original list is : " + str (test_list)) # Double List using map() function res = list ( map ( lambda x: x + x, test_list)) # printing result print ( "Double List is : " + str (res)) |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
Time complexity: The time complexity of this program is O(n), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space used by this program is O(n), where n is the length of the input list test_list.
Method #6:Using Numpy
Algorithm
- Import the numpy library.
- Create a list of integers test_list.
- Call the multiply() function from numpy library and pass test_list and 2 as arguments. The multiply() function will perform element-wise multiplication of the input list by 2 and return a new list.
- Assign the new list to double_list.
- Print the original list test_list.
- Print the double list double_list
Python3
import numpy as np test_list = [ 12 , 67 , 98 , 34 , 43 ] double_list = np.multiply(test_list, 2 ) print ( "Original list:" , test_list) print ( "Double list:" , double_list) #This code is contributed by Vinay Pinjala. |
Output
Original list: [12, 67, 98, 34, 43] Double list: [ 24 134 196 68 86]
Time complexity: O(n), where n is the length of the input list test_list. This is because the multiply() function has to iterate over all the elements of test_list once to perform element-wise multiplication.
Auxiliary Space: O(n), where n is the length of the input list test_list. This is because the multiply() function creates a new list of the same size as test_list to store the result of the element-wise multiplication.
Method #7:Using reduce() and lambda:
Algorithm :
- Import the reduce function from the functools module.
- Create a list test_list containing integers. Call the reduce function with three argumen the reduce function with three arguments:
- A lambda function that takes two arguments, x and y, and returns a new list that concatenates the current value of x with a list containing y*2.
- The list to reduce, test_list.
- An empty list as the initial value for x.
- Assign the result of reduce to a variable res.
- Print the original list and the resulting list.has context menu
Python3
from functools import reduce # Initializing list test_list = [ 12 , 67 , 98 , 34 , 43 ] # printing original list print ( "The original list is : " + str (test_list)) res = reduce ( lambda x,y: x + [y * 2 ], test_list, []) # printing result print ( "Double List is : " + str (res)) #This code is contributed by Jyothi Pinjala. |
The original list is : [12, 67, 98, 34, 43] Double List is : [24, 134, 196, 68, 86]
The time complexity of this code : O(n), where n is the length of the input list. This is because both the list comprehension and reduce function iterate over each element of the input list exactly once.
The auxiliary space of this code : O(n), as a new list is created to store the doubled elements of the input list. In the case of the reduce function, a new list is also created to accumulate the results at each iteration. However, the space used by these additional lists is negligible compared to the space used by the input list.