python - FIltering more then one string with list comprehension -
i working list containing urls , wanting filter out extentions .jpg, jpeg , .png.
i tried use list comprehension: [elem elem in li if elem != ".jpg"] filter 1 string.
is there way solve this?
thanks.
instead of checking equality of element single string, check if element present in set, each member of set string want match against:
blacklist = set(['.jpg', '.jpeg', '.png']) filtered = [elem elem in li if elem not in blacklist]
however, mentioned you're trying filter extension out of list of urls, suggests need check see if string ends with extension, not if whole string equal extension. in case, you'd want this:
blacklist = ('.jpg', '.jpeg', '.png') filtered = [elem elem in li if not elem.endswith(blacklist)]
this makes sure elem
doesn't end of items in blacklist
.
Comments
Post a Comment