-3

I have:

my_column = [('Me ',), ('If ',), ('Will ',), ('If ',)]

I want to turn this into a simple list of strings:

['Me','If','Will','If']
daniel
  • 37
  • 2
  • 9
  • What have you tried for this? – Zebartin Jul 04 '21 at 13:48
  • Does this answer your question? [in Python, How to join a list of tuples into one list?](https://stackoverflow.com/questions/15269161/in-python-how-to-join-a-list-of-tuples-into-one-list) – astentx Jul 05 '21 at 13:00

3 Answers3

3

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 ']
chepner
  • 497,756
  • 71
  • 530
  • 681
2

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 '}
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
0
my_column = [('Me ',), ('If ',), ('Will ',), ('If ',)]
list=list()
for i in my_column:
    for j in i:
        list.append(j)
print(list)

  • 3
    Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Jul 04 '21 at 23:44