Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Check list elements in a string in python


I have a String and a list as follows.

mystring = "ABC company is good. Also (ALDs) not bad. Sometimes FLJ is bad. I like NYG"
mylist = ['ALD', 'ALDs', 'NMN', 'YEW', 'ABC', 'NYGs']

Now I want to append '_ABB' for every word that appears in the list. For example in the above case I should get an output as follows.

mystring = "ABC_ABB company is good Also ALDs_ABB not bad Sometimes FLJ is bad I like NYG"

I am currently doing it as follows.

splits = mystring.split()
newstring = []
for item in splits:
if item in mylist:
item = item+'_ABB'

for ele in newstring:
# ' '.join(ele) to get the final completed string

I want to know if it is possible to do this in one line using python?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Check list elements in a string in python


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription