Python | Repeat each element K times in list
Many times we have this particular use-case in which we need to repeat each element of list K times. The problems of making a double clone have been discussed but this problem extends to allow a flexible variable to define the number of times the element has to be repeated. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension
This particular task requires generally 2 loops and list comprehension can perform this particular task in one line and hence reduce the lines of codes and improving code readability.
# Python3 code to demonstrate # repeat element K times # using list comprehension # initializing list of lists test_list = [4, 5, 6] # printing original list print("The original list : " + str(test_list)) # declaring magnitude of repetition K = 3 # using list comprehension # repeat elements K times res = [ele for ele in test_list for i in range(K)] # printing result print("The list after adding elements : " + str(res)) |
The original list : [4, 5, 6] The list after adding elements : [4, 4, 4, 5, 5, 5, 6, 6, 6]
Method #2 : Using itertools.chain.from_iterable() + itertools.repeat()
This particular problem can also be solved using python inbuilt functions of itertools library. The repeat function, as name suggests does the task of repetition and grouping into a list is done by the from_iterable function.
# Python3 code to demonstrate # repeat element K times # using itertools.chain.from_iterable() + itertools.repeat() import itertools # initializing list of lists test_list = [4, 5, 6] # printing original list print("The original list : " + str(test_list)) # declaring magnitude of repetition K = 3 # using itertools.chain.from_iterable() # + itertools.repeat() repeat elements K times res = list(itertools.chain.from_iterable(itertools.repeat(i, K) for i in test_list)) # printing result print("The list after adding elements : " + str(res)) |
The original list : [4, 5, 6] The list after adding elements : [4, 4, 4, 5, 5, 5, 6, 6, 6]
Recommended Posts:
- Python | Check if any element occurs n times in given list
- Python | Repeat and Multiply list extension
- Python | Add similar value multiple times in list
- Python | Remove elements of list that are repeated less than k times
- Python | Check if k occurs atleast n times in a list
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- numpy.repeat() in Python
- Python | Pandas Index.repeat()
- Python | Repeat String till K
- Python | Pandas Series.str.repeat()
- Python | Pandas Series.repeat()
- Python | Numpy matrix.repeat()
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.



