Python | Append Odd element twice
Last Updated :
15 Mar, 2023
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
# Python code to create a new list from initial list
# with condition to append every odd element twice.
# List initialization
Input = [1, 2, 3, 8, 9, 11]
# Using list comprehension
Output = [elem for x in Input for elem in (x, )*(x % 2 + 1)]
# printing
print("Initial list is:'", Input)
print("New list is:", Output)
OutputInitial 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
# Python code to create a new list from initial list
# with condition to append every odd element twice.
# Importing
from itertools import chain
# List initialization
Input = [1, 2, 3, 8, 9, 11]
# Using list comprehension and chain
Output = list(chain.from_iterable([i]
if i % 2 == 0 else [i]*2 for i in Input))
# printing
print("Initial list is:'", Input)
print("New list is:", Output)
OutputInitial 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
# Python code to create a new list from initial list
# with condition to append every odd element twice.
# Importing
import numpy as np
# List initialization
Input = [1, 2, 3, 8, 9, 11]
Output = []
# Using Numpy repeat
for x in Input:
(Output.extend(np.repeat(x, 2, axis = 0))
if x % 2 == 1 else Output.append(x))
# printing
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
# Python code to create a new list from initial list
# with condition to append every odd element twice.
# List initialization
Input = [1, 2, 3, 8, 9, 11]
Output = []
for i in Input:
if(i%2!=0):
Output.extend([i]*2)
else:
Output.append(i)
# printing
print("Initial list is:", Input)
print("New list is:", Output)
OutputInitial 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):
# Base case: if the input list is empty, return an empty list
if not input_list:
return []
else:
# Recursive case: if the first element of the input list is odd,
# append it twice to the output list, otherwise append it once.
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:])
# List initialization
input_list = [1, 2, 3, 8, 9, 11]
# Call the recursive function to create the new list
output_list = append_odd_twice(input_list)
# Print the results
print("Initial list is:", input_list)
print("New list is:", output_list)
OutputInitial 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.
Similar Reads
Python List append() Method append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this.Pythona = [2, 5, 6, 7] # Use append() to add the element 8 to the end of the list a.append(8) print(a)O
3 min read
Python | Pandas Series.append() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.append() function is used to
4 min read
Add Element to Tuple Tuples in Python are immutable, meaning once you create them, you cannot change, add, or remove elements directly. But donât worry! There are ways to add elements to a tuple, like creating a new tuple by combining the original one with the new element. Let's dive into how you can do this in differen
3 min read
Set add() Method in Python The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot
4 min read
Python List extend() Method In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once.Letâs look at a simple
2 min read
Python | Pandas TimedeltaIndex.append() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.append() function appends a collection of index options together
2 min read