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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 the function to perform the addition of a pair of similar numbers simultaneously in the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.