[['2.0'], ['1.5'], ['1.4'], ['2.1'], ['2.2'], ['2.4'], ['3.0'], ['1.1'], ['1.0'], ['1.6']]
and I need it to be ['2.0','1.5', etc]
How to fix it?
[['2.0'], ['1.5'], ['1.4'], ['2.1'], ['2.2'], ['2.4'], ['3.0'], ['1.1'], ['1.0'], ['1.6']]
and I need it to be ['2.0','1.5', etc]
How to fix it?
Simply use list comprehension with sequence assignment:
final_list = [x for x, in original_list]
Mind the comma (,
) in the list comprehension, which is essential.
Here we make the assumption that there is only one element in each sublist. Python will check this and error if it is not the case. This can be desired behavior (or perhaps you want to concatenate the elements).
Example:
>>> original_list = [['2.0'], ['1.5'], ['1.4'], ['2.1'], ['2.2'], ['2.4'], ['3.0'], ['1.1'], ['1.0'], ['1.6']]
>>> final_list = [x for x, in original_list]
>>> final_list
['2.0', '1.5', '1.4', '2.1', '2.2', '2.4', '3.0', '1.1', '1.0', '1.6']
You can try something like this :
item_list = [['2.0'], ['1.5'], ['1.4'], ['2.1'], ['2.2'], ['2.4'], ['3.0'], ['1.1'], ['1.0'], ['1.6']]
print([item for sub_list in item_list for item in sub_list])
This will result in :
['2.0', '1.5', '1.4', '2.1', '2.2', '2.4', '3.0', '1.1', '1.0', '1.6']