Extract list item based on condition


Hi i am new to python, i am stuck in a code.
Please help

i have a list,
example :
my_list = [“he is a politician”, “i am an engineer”, “whats in the name”, “happy new year”]

here what i want to do is :
i want to store each list element based on condition

if list has “politician” then store corresponding list item “he is a politician” in a variable.
if it has "name " then store corresponding list item "whats in the name " in other variable .

any help is appreciated.


you can simply loop over my_list, then see if the word (politician for example) is in the list element, if so, add this string to a variable.


thank you very much, it worked


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.