Change language

Python | Repeating an item in a list

|

Method # 1: Using List Comprehension
In this method, we simply loop twice for each value and add to the new list. This is just a concise alternative to the naive method.

 ` # Python3 demo code ` ` # duplicate the element ` ` # using comprehension list `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 3 ` `, ` ` 9 ` `] `   ` # print original list ` ` pri nt ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # using comprehension list ` ` # duplicate element ` ` res ` ` = ` ` [i ` ` for ` ` i ` ` in ` ` test_list ` ` for ` ` x ` ` in ` ` (` ` 0 ` `, ` ` 1 ` `)] `   ` # print 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 redu () + add
We can also use the Reduce function to use a function to simultaneously add a pair of similar numbers to the list.

 ` # Python3 demo code ` ` # duplicate the element ` ` # using Reduce () + add ` ` from ` ` operator ` ` import ` ` add ` ` `  ` # initializing list ` ` test_list ` ` = ` ` [` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 3 ` `, ` ` 9 ` `] `   ` # print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # using Reduce () + add # duplicate the element `` res = list ( reduce (add, [(i, i) for i in test_list]))   # print 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 ` i tertools.chain (). from_iterable () `
The from_iterable function can also be used to accomplish this task of adding a duplicate. It just creates a pair of each repeating element and inserts it sequentially.

 ` # Python3 demo code ` ` # duplicate the element ` ` # use itertools.chain.from_iterable () ` ` import ` ` itertools `   ` # initialization list ` ` test_list ` ` = ` ` [` ` 4 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 3 ` `, ` ` 9 ` `] ` ` `  ` # print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # using itertools.chain.from_iterable () ` ` # duplicate element ` ` res ` ` = ` ` list ` ` (itertools.chain.from_iterable ([i, i] ` ` for ` ` i ` ` in ` ` test_list)) `   ` # print result ` ` print ` ` (< / code> "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] `

Shop

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method