Extract only first element of tuple

Question:

I have a column on my data containing a list of two elements of tuples from POS tagging. I want to extract only the first element of these tuples and append it to another column. All code I found can only be applied to a single list of tuples. I would like to loop the code for every row as I have more than 100 rows.

pos_tag_clean word_clean
[(semoga, SC), (saja, RB), (di, IN), (sini, PR), (bisa, MD), (cepat, JJ), (cair, NN), (semoga, NN), (saja, RB), (ini, PR), (beneran, NN), (ada, VB), (nya, NN), (bantuan, NN), (buat, JJ), (butuh, VB), (banget, NN)] [semoga, saja, di, sini, bisa, cepat, cair, semoga, saja, ini, beneran, ada, nya, bantuan, buat, butuh, banget]
[(kak, VB), (kenapa, WH), (perbaikan, NN), (sistem, NN), (nya, PRP), (tidak, NEG), (selesai, VB)] [kak, kenapa, perbaikan, sistem, nya, tidak, selesai]
[(sangat, RB), (baik, JJ)] [sangat, baik]

Can you help me solve this?

Asked By: Christabel

||

Answers:

Well, if each row is stored in pos_tag_clean, you could do a list comprehension:

word_clean = [[item[0] for item in row] for row in pos_tag_clean]
Answered By: definite_d
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.