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 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 the function to perform the addition of a pair of similar numbers simultaneously in the list.
# 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 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]
Recommended Posts:
- Python | Replace elements in second list with index of same element in first list
- Python | Slicing list from Kth element to last element
- Python | How to get the last element of list
- Python | Move element to end of the list
- Python | Last occurrence of some element in a list
- Python | Removing first element of list
- Python | Operation to each element in list
- Python | Operate on every Kth element of list
- Python | Insert after every Nth element in a list
- Python | Add the element in the list with help of indexing
- Python | Element with largest frequency in list
- Python | Get first element with maximum value in list of tuples
- Python | Shift last element to first position in list
- Python | Test if string contains element from list
- Python | Count occurrences of an element in a list
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.



