>>> a = [[1.0, 1.2, 1.4], [2.0, 2.2], 5.0]
>>> ' '.join(str(s) for x in a for s in (x if isinstance(x, list) else [x]))
'1.0 1.2 1.4 2.0 2.2 5.0'
This is equivalent in behavior to the following for loop:
tmp = []
for x in a:
if isinstance(x, list):
for s in x:
tmp.append(s)
else:
tmp.append(x)
result = ' '.join(tmp)
Note that this assumes only one level of nesting, as in your example. If you need this to work with any arbitrary iterable instead of only lists you can use collections.Iterable
, as long as your values are not strings (otherwise it will iterate over the characters in the string).