Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Append Odd element twice

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




# 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)


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




# 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)


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




# 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)


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.

  1. Define a function append_odd_twice that takes an input list as its argument.
  2. If the input list is empty, return an empty list (base case).
  3. Otherwise, if the first element of the input list is odd, append it twice to the output list, otherwise append it once.
  4. Recursively call the function with the rest of the input list and append the result to the output list.
  5. 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)


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments