Python Practice › Tuple Programs › Program 55
Insert a string after every element
Write a Python program to convert a tuple into a list with a string after every element.
Input
(1, 2, 3, 4), "sqatools"
Output
[1, 'sqatools', 2, 'sqatools', 3, 'sqatools', 4, 'sqatools']
Approach
- For each item add the item and the string.
Solution
t = (1, 2, 3, 4)word = "sqatools"print([x for item in t for x in (item, word)])Output
[1, 'sqatools', 2, 'sqatools', 3, 'sqatools', 4, 'sqatools']
Learn the concept
New to this topic? Read the lesson, then come back.