In this python tuple program, we will flatten a list of tuples into a string.
Steps to solve the program
- Take a list of tuples as input and create an empty string.
- Use a for loop to iterate over the tuples in the list.
- Use a nested for loop to iterate over characters in the tuples.
- Add the characters to the string.
- Print the output.
l = [('s','q','a'),('t','o'),('o','l','s')]
print("Original list of tuples: ",l)
str1 = ""
for tup in l:
for char in tup:
str1 += char+" "
print("New string: ",str1)
Output :
Original list of tuples: [('s', 'q', 'a'), ('t', 'o'), ('o', 'l', 's')]
New string: s q a t o o l s