Python | Element repetition in list
Sometimes we require to add a duplicate value in the list for several different utilities. This type of application is sometimes required in day-day programming. Let’s discuss certain ways in which we add a clone of a number to its next position.
Method #1 : Using list comprehension In this method, we just iterate the loop twice for each value and add to the desired new list. This is just a shorthand alternative to the naive method.
Python3
# Python3 code to demonstrate # to perform element duplication # using list comprehension # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using list comprehension # to perform element duplication res = [i for i in test_list for x in ( 0 , 1 )] # printing result print ("The list after element duplication " + str (res)) |
Output :
The original list is : [4, 5, 6, 3, 9] The list after element duplication [4, 4, 5, 5, 6, 6, 3, 3, 9, 9]
Method #2 : Using reduce() + add We can also use the reduce function to act the function to perform the addition of a pair of similar numbers simultaneously in the list.
Python3
# Python3 code to demonstrate # to perform element duplication # using reduce() + add from operator import add # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using reduce() + add # to perform element duplication res = list ( reduce (add, [(i, i) for i in test_list])) # printing result print ("The list after element duplication " + str (res)) |
Output :
The original list is : [4, 5, 6, 3, 9] The list after element duplication [4, 4, 5, 5, 6, 6, 3, 3, 9, 9]
Method #3 : Using itertools.chain().from_iterable() from_iterable function can also be used to perform this task of adding a duplicate. It just makes the pair of each iterated element and inserts it successively.
Python3
# Python3 code to demonstrate # to perform element duplication # using itertools.chain.from_iterable() import itertools # initializing list test_list = [ 4 , 5 , 6 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # using itertools.chain.from_iterable() # to perform element duplication res = list (itertools.chain.from_iterable([i, i] for i in test_list)) # printing result print ("The list after element duplication " + str (res)) |
Output :
The original list is : [4, 5, 6, 3, 9] The list after element duplication [4, 4, 5, 5, 6, 6, 3, 3, 9, 9]
Method #4 : Using repeat
You can use the repeat method of the itertools module to achieve the desired result of adding a duplicate element to the list. Here is how you can do it:
Python3
from itertools import repeat # Initializing the list test_list = [ 4 , 5 , 6 , 3 , 9 ] # Printing the original list print ( "The original list is:" , test_list) # Using the repeat method to add a duplicate element res = [i for i in test_list for _ in repeat( None , 2 )] # Printing the result print ( "The list after element duplication:" , res) #This code is contributed by Edula Vinay Kumar Reddy |
The original list is: [4, 5, 6, 3, 9] The list after element duplication: [4, 4, 5, 5, 6, 6, 3, 3, 9, 9]
Time complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...