Python Practice › Tuple Programs › Program 53

Flatten a list of lists into a tuple

EasyTuple Programs · 53 of 70

Write a Python program to flatten a list of lists into a tuple.

Advertisement
Input
[["s"], ["q"], ["a"], ["t"], ["o"], ["o"], ["l"], ["s"]]
Output
('s', 'q', 'a', 't', 'o', 'o', 'l', 's')

Approach

  1. A nested generator expression.

Solution

Python
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')
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs