Python Practice › Tuple Programs › Program 53
Flatten a list of lists into a tuple
Write a Python program to flatten a list of lists into a tuple.
Input
[["s"], ["q"], ["a"], ["t"], ["o"], ["o"], ["l"], ["s"]]
Output
('s', 'q', 'a', 't', 'o', 'o', 'l', 's')Approach
- A nested generator expression.
Solution
data = [["s"], ["q"], ["a"], ["t"], ["o"], ["o"], ["l"], ["s"]]print(tuple(x for sub in data for x in sub))Output
('s', 'q', 'a', 't', 'o', 'o', 'l', 's')Learn the concept
New to this topic? Read the lesson, then come back.