Sometimes while working with Python, we can have a problem in which we need to perform the repetion of each string in list and also attach a deliminator to each occurrence. This kind of problem can occur in day-day programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This task can be performed in brute force way using loop. In this, we iterate the list and perform string addition and multiplication while iteration using suitable operators.
The original list is : ['gfg', 'is', 'best'] List after performing operations : ['gfg-gfg-gfg-', 'is-is-is-', 'best-best-best-']
Method #2 : Using
join() + list comprehension
The combination of above functionalities can also be used to perform this task. In this, we perform the task of attaching delim using join() and list comprehension performs the task of repetition. Avoids trailing delim.
The original list is : ['gfg', 'is', 'best'] List after performing operations : ['gfg-gfg-gfg', 'is-is-is', 'best-best-best']
- Python - Incremental and Cyclic Repetition of List Elements
- Python | Element repetition in list
- Randomly select elements from list without repetition in Python
- PyQt5 – Add spacing between indicator and Check box
- PyQt5 - How to change the spacing between indicator and text part of Radio Button ?
- Python - Custom Consecutive character repetition in String
- PyQt5 QSpinBox - Getting letter spacing type
- PyQt5 QSpinBox - Getting letter spacing
- PyQt5 QSpinBox - Setting letter spacing
- PyQt5 QSpinBox - Setting word spacing
- PyQt5 QSpinBox - Getting word spacing
- PyQt5 QSpinBox - Getting the line spacing value of the text
- PyQt5 QListWidget – Setting Spacing Between the Items
- PyQt5 QListWidget – Getting Spacing Between the Items
- Python - Consecutive Repetition of Characters
- Python - Custom element repetition
- Python | Convert List of String List to String List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert a string representation of list into 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 firstname.lastname@example.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.