Python – Consecutive Repetition of Characters

Sometimes, while working with character lists we can have a problem in which we need to perform consecutive repetition of characters. This can have application in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension
This is one of the way in which this task can be performed. In this, we perform a brute force way to perform but in a one-liner for by multiplying each character by magnitude.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive Repetition of  Characters
# Using list comprehension
  
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 3
  
# Consecutive Repetition of  Characters
# Using list comprehension
res = [sub for ele in test_list for sub in [ele] * K]
  
# printing result 
print("The list after Consecutive Repetition is : " + str(res)) 

chevron_right


Output :

The original list is : [‘g’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’]
The list after Consecutive Repetition is : [‘g’, ‘g’, ‘g’, ‘f’, ‘f’, ‘f’, ‘g’, ‘g’, ‘g’, ‘i’, ‘i’, ‘i’, ‘s’, ‘s’, ‘s’, ‘b’, ‘b’, ‘b’, ‘e’, ‘e’, ‘e’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’]

 



Method #2 : Using chain() + repeat()
The combination of above functions can be used to solve this problem. In this, we perform the task of repeating using repeat() and the result construction using chain().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive Repetition of  Characters
# Using chain() + repeat()
from itertools import chain, repeat
  
# initializing list
test_list = ['g', 'f', 'g', 'i', 's', 'b', 'e', 's', 't']
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 3
  
# Consecutive Repetition of  Characters
# Using chain() + repeat()
res = list(chain.from_iterable(repeat(chr, K) for chr in test_list))
  
# printing result 
print("The list after Consecutive Repetition is : " + str(res)) 

chevron_right


Output :

The original list is : [‘g’, ‘f’, ‘g’, ‘i’, ‘s’, ‘b’, ‘e’, ‘s’, ‘t’]
The list after Consecutive Repetition is : [‘g’, ‘g’, ‘g’, ‘f’, ‘f’, ‘f’, ‘g’, ‘g’, ‘g’, ‘i’, ‘i’, ‘i’, ‘s’, ‘s’, ‘s’, ‘b’, ‘b’, ‘b’, ‘e’, ‘e’, ‘e’, ‘s’, ‘s’, ‘s’, ‘t’, ‘t’, ‘t’]




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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.