Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define lemmatizer function in a for loop to a single print function statement in python

I need to add the function within the print function to a variable to be calledd to be printed only from the variable name.

My code –
for w in processed_H2_tag: print(lemmatizer.lemmatize(w.lower(), pos=wordnet.VERB))

Expected – Print(output)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"Output" is to be defined

>Solution :

You mean how to instead of printing get all the values into a list which you can then print?

You can do that with a list comprehension:

output = [lemmatizer.lemmatize(w.lower(), pos=wordnet.VERB) for w in processed_H2_tag]

Or if you prefer a classic for loop:

output = []
for w in processed_H2_tag:
    output.append(lemmatizer.lemmatize(w.lower(), pos=wordnet.VERB))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading