Python – Append given number with every element of the list
Given a list and a number, write a Python program to append the number with every element of the list.
Examples:
input = [1,2,3,4,5]
key = 5 (number to be appended)
output = [1,5,2,5,3,5,4,5,5,5]
The following are ways to achieve this functionality:
Method 1: Using for loop
Python
input = [ 1 , 2 , 3 , 4 , 5 ] key = 7 result = [] for ele in input : result.append(ele) result.append(key) print (result) |
Output:
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
Time complexity: O(n)
Space complexity: O(n)
Method 2: Using list comprehension and itertools.chain
Python
import itertools input = [ 1 , 2 , 3 , 4 , 5 ] key = 7 result = list (itertools.chain( * [[ele, key] for ele in input ])) print (result) |
Output:
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using zip and for loop
Python
input = [ 1 , 2 , 3 , 4 , 5 ] key = 7 result = [] for x, y in zip ( input , [key] * len ( input )): result.extend([x, y]) print (result) |
Output:
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
Time Complexity: O(n)
Space Complexity: O(n)
Method 4: Using list(),map(),str(),join() and split()
Python3
input = [ 1 , 2 , 3 , 4 , 5 ] key = 7 l = list ( map ( str , input )) p = "*" + str (key) + "*" x = p.join(l) a = x.split( "*" ) res = list ( map ( int ,a)) res.append(key) print (res) |
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
Time complexity: O(n)
Space complexity: O(n)
Method 5: Using pandas
Python3
import pandas as pd input = [ 1 , 2 , 3 , 4 , 5 ] key = 7 df = pd.DataFrame({ 'col' : input }) result = df[ 'col' ]. apply ( lambda x: [x, key]). sum () print (result) #This code is contributed by Vinay pinjala. |
Output:
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
Time complexity: O(n)
Space complexity: O(n)
Method#6: Using Recursive method.
Algorithm:
The recursive_method function takes two inputs, an input_list of integers and a key integer. It first checks if the input_list is empty or not. If it is empty, it returns an empty list. If it is not empty, it takes the first element of the input_list, adds it to the key, and creates a new list with these two elements. It then recursively calls the recursive_method function on the rest of the input_list (i.e., input_list[1:]) and concatenates the result of this recursive call to the new list it created earlier. This process continues until the entire input_list has been processed.
Python3
def recursive_method(input_list, key): if not input_list: return [] else : return [input_list[ 0 ], key] + recursive_method(input_list[ 1 :], key) input_list = [ 1 , 2 , 3 , 4 , 5 ] key = 7 result = recursive_method(input_list, key) print (result) |
[1, 7, 2, 7, 3, 7, 4, 7, 5, 7]
The time complexity of this recursive_method function is O(n), where n is the length of the input_list. This is because for each element in the input_list, the function performs a constant amount of work (i.e., creating a list with two elements and making a recursive call on a sublist of length n-1). Therefore, the total number of operations is proportional to the length of the input_list.
The auxiliary space of this function is also O(n), because at each recursive call, a new list is created and stored in memory. The maximum number of recursive calls that can be made is equal to the length of the input_list, so the total amount of space used is proportional to the length of the input_list
Please Login to comment...