Related Articles

# Python | Element repetition in list

• Last Updated : 26 Jan, 2019

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]
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up