![]() |
VOOZH | about |
Given a list of numbers, the task is to create a new list from the initial list with the condition to append every odd element twice. Below are some ways to achieve the above task.
Method #1: Using list comprehension
Initial list is:' [1, 2, 3, 8, 9, 11] New list is: [1, 1, 2, 3, 3, 8, 9, 9, 11, 11]
Time Complexity: O(n), Here n is the size of the list.
Auxiliary Space: O(n)
Method #2: Using itertools
Initial list is:' [1, 2, 3, 8, 9, 11] New list is: [1, 1, 2, 3, 3, 8, 9, 9, 11, 11]
Time Complexity: O(n), Here n is the size of the list.
Auxiliary Space: O(n)
Method #3: Using Numpy array
Output:
Initial list is: [1, 2, 3, 8, 9, 11] New list is: [1, 1, 2, 3, 3, 8, 9, 9, 11, 11]
Time Complexity: O(n), Here n is the size of the list.
Auxiliary Space: O(n)
Method #4 : Using extend() method
Initial list is:' [1, 2, 3, 8, 9, 11] New list is: [1, 1, 2, 3, 3, 8, 9, 9, 11, 11]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method#5: Using Recursive method.
The algorithm creates a new list from the input list with the condition to append every odd element twice, using a recursive function.
Initial list is: [1, 2, 3, 8, 9, 11] New list is: [1, 1, 2, 3, 3, 8, 9, 9, 11, 11]
The time complexity of the algorithm is O(n), where n is the length of the input list. The recursive function performs a constant amount of work for each element in the list.
The space complexity of the algorithm is also O(n), since the recursive function creates a new list for each recursive call.