JuniorCodeOccasionalNot answered yet
Convert a list of one-element tuples into a list of strings
Given a list of single-element tuples, return a flat list of the inner values.
data = [('as_admin1',), ('as_admin2',), ('as_admin3',)]
# want: ['as_admin1', 'as_admin2', 'as_admin3']
def flatten(data: list) -> list:
# your code here
Write the implementation.
Take the first element of each tuple: [t[0] for t in data]. A one-element tuple is written ('x',) with a trailing comma, so indexing [0] pulls the single value out. Equivalent forms are [v for (v,) in data] (tuple unpacking in the loop target) or list(map(lambda t: t[0], data)).
- ✗Forgetting a one-element tuple needs
[0]to unwrap it - ✗Thinking
str(t)cleanly yields the inner value - ✗Confusing the outer list index with the inner tuple index
- →How would you flatten tuples that may hold more than one value?
- →Why does
('x')differ from('x',)?