Python | Append Odd element twice
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
Python3
Input = [ 1 , 2 , 3 , 8 , 9 , 11 ]
Output = [elem for x in Input for elem in (x, ) * (x % 2 + 1 )]
print ( "Initial list is:'" , Input )
print ( "New list is:" , Output)
|
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 #2: Using itertools
Python3
from itertools import chain
Input = [ 1 , 2 , 3 , 8 , 9 , 11 ]
Output = list (chain.from_iterable([i]
if i % 2 = = 0 else [i] * 2 for i in Input ))
print ( "Initial list is:'" , Input )
print ( "New list is:" , Output)
|
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 #3: Using Numpy array
Python3
import numpy as np
Input = [ 1 , 2 , 3 , 8 , 9 , 11 ]
Output = []
for x in Input :
(Output.extend(np.repeat(x, 2 , axis = 0 ))
if x % 2 = = 1 else Output.append(x))
print ("Initial list is :'", Input )
print ("New list is :", Output)
|
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
Python3
Input = [ 1 , 2 , 3 , 8 , 9 , 11 ]
Output = []
for i in Input :
if (i % 2 ! = 0 ):
Output.extend([i] * 2 )
else :
Output.append(i)
print ( "Initial list is:" , Input )
print ( "New list is:" , Output)
|
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)
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.
- Define a function append_odd_twice that takes an input list as its argument.
- If the input list is empty, return an empty list (base case).
- Otherwise, if the first element of the input list is odd, append it twice to the output list, otherwise append it once.
- Recursively call the function with the rest of the input list and append the result to the output list.
- Return the output list.
Python3
def append_odd_twice(input_list):
if not input_list:
return []
else :
if input_list[ 0 ] % 2 = = 1 :
return [input_list[ 0 ], input_list[ 0 ]] + append_odd_twice(input_list[ 1 :])
else :
return [input_list[ 0 ]] + append_odd_twice(input_list[ 1 :])
input_list = [ 1 , 2 , 3 , 8 , 9 , 11 ]
output_list = append_odd_twice(input_list)
print ( "Initial list is:" , input_list)
print ( "New list is:" , output_list)
|
Output
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.
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...