I have:
my_column = [('Me ',), ('If ',), ('Will ',), ('If ',)]
I want to turn this into a simple list of strings:
['Me','If','Will','If']
I have:
my_column = [('Me ',), ('If ',), ('Will ',), ('If ',)]
I want to turn this into a simple list of strings:
['Me','If','Will','If']
Use itertools.chain
.
>>> from itertools import chain
>>> list(chain(*my_column))
['Me ', 'If ', 'Will ', 'If ']
or
>>> list(chain.from_iterable(my_column))
['Me ', 'If ', 'Will ', 'If ']
You can use list comprehension to convert a list of tuples into a list.
pairs = [('Me ',), ('If ',), ('Will ',), ('If ',)]
# using list comprehension
out = [item for t in pairs for item in t]
print(out)
Output:
['Me ', 'If ', 'Will ', 'If ']
If want to remove duplicates then replace [] notation with {} to create a set.
out = {item for t in a for item in t}
Output:
{'Me ', 'If ', 'Will '}
my_column = [('Me ',), ('If ',), ('Will ',), ('If ',)]
list=list()
for i in my_column:
for j in i:
list.append(j)
print(list)