John Davis
John Davis

Reputation: 303

remove particular word from a list

My list is given below -

mylist = [("aaaa8"),("bb8_null"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),("abc_null"),
         ("tre_null"),("fdsf"),("ccc8"),("dddddd8")]

I want my final list should be like -

final_list = [("aaaa8"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),
         ("fdsf"),("ccc8"),("dddddd8")]

I have done so far -

final_list = [i.replace('_null', "") for i in mylist]

But it's not working

Upvotes: 0

Views: 28

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Looks like you need str.endswith.

Ex:

mylist = [("aaaa8"),("bb8_null"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),("abc_null"),
         ("tre_null"),("fdsf"),("ccc8"),("dddddd8")]
final_list = [i for i in mylist if not i.endswith('_null')]
print(final_list)
# --> ['aaaa8', 'ccc8', 'dddddd8', 'aaaa8', 'hsd', 'ccc8', 'fdsf', 'ccc8', 'dddddd8']

Upvotes: 2

Related Questions