0

I have returned a list of href values from a HTML document. I want to go though every link within this list and test to see if they contain any of the values within my IMAGE_FORMAT tuple.

IMAGE_FORMAT = (
    '.png',
    '.jpg',
    '.jpeg',
    '.gif',
)

At present I am simply testing for '.jpg' e.g if '.jpg' in link.get('href'):

I'd like to extend this code to something along the lines of if [any value inside IMAGEFORMAT] in link.get('href'):

What would be the most efficient or cleanest way or doing so?

2 Answers 2

6

If you really want in, then maybe

href = link.get('href')
if any(end in href for end in IMAGE_FORMAT):
    # do something
    pass

but if you actually want ends with, then use .endswith:

>>> IMAGE_FORMAT = ('.png','.gif','.jpg','.jpeg')
>>> 'fred.gif'.endswith(IMAGE_FORMAT)
True

Depends on how you want to treat 'fred.gif.gz', etc. Also note you might want to work with href.lower() if you don't care about case.

Sign up to request clarification or add additional context in comments.

2 Comments

I always find myself tempted to write any('fred.gif'.endswith(ending) for ending in IMAGE_FORMAT), neglecting that .endswith already accepts *args to do what I want. x.x
You mention If you really want in, then maybe... would there be a nicer way of writing the same code?
1

Try any against list comprehension.

any(e in href for e in IMAGE_FORMAT)

Or, in English, "are any of the items in my image formats in my URI?" Bare in mind how in functions with strings, though.

1 Comment

No need for the list. any(e in href for e in IMAGE_FORMAT) will do fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.