Search code examples
pythonlisttuplesmulti-index

Python remove tuples and merge the two values from tuples inside them to one item


I have list similar to this:

[('name', ''),
 ('season', ''),
 ('company', ''),
 ('date', ''),
 ('mean value', 1),
 ('mean value', 2),
 ('mean value', 3),
 ('mean value', 4)]

I would like to get rid of the tuples and to merge between the two values if exists, to get the following list:

['name',
 'season',
 'company',
 'date',
 'mean value 1',
 'mean value 2',
 'mean value 3',
 'mean value 4']

I'm not sure how to do this, looking for ways to do this efficiently.


Solution

  • If you want to add in a space (mean value 4):

    [f"{a} {b}".strip() for a, b in lst]
    

    If you don't need a space ('mean value4'):

    [f"{a}{b}" for a, b in lst]
    

    Note: This answer only works for tuples of length 2