In this python tuple program, we will flatten a list of lists into a tuple.
Steps to solve the program
- Take a list of lists as input and create an empty list.
- Use a for loop to iterate over the list in the lists.
- Use a nested for loop to iterate over elements in each list.
- Add elements to the empty list using append().
- Convert the new list to a tuple using tuple().
- Print the output.
l = [['s'],['q'],['a'],['t'],['o'],['o'],['l'],['s']]
print("List of lists: ",l)
l1 = []
for lists in l:
for char in lists:
l1.append(char)
tup = tuple(l1)
print("Tuple: ",tup)
Output :
List of lists: [['s'], ['q'], ['a'], ['t'], ['o'], ['o'], ['l'], ['s']]
Tuple: ('s', 'q', 'a', 't', 'o', 'o', 'l', 's')