1

I have a Python Condition Below That I need to write it in different Lines How Can I Do it?

if image_finder[0].find_all('img')[0]['src'].replace('//','https://') == 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png' or image_finder[0].find_all('img')[0]['src'].replace('//','https://') =='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png':
    print(x)

4 Answers 4

2

We are going to create some variables to keep everything in order.

fixedLink = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
option1 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
option2 = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'

First option (my recommendation)

if fixedLink in (option1, option2):
    print(x)

Second option

if fixedLink == option1 or fixedLink == option2:
    print(x)
Sign up to request clarification or add additional context in comments.

4 Comments

Third option (as the number of options grows): if any(fixedLink == x for x in (option1, option2)):
Or if fixedLink in {option1, option2} for O(1) lookup, although it's not really relevant, not even for more values.
@chepner Am I missing something or is this just a more complicated (and probably slightly slower) version of if fixedLink in (option1, option2)?
Slightly slower, but far more readable if you actually have, say, 10 URLs to compare against. With only two I wouldn't bother, but I would probably switch to any even with only three.
1

What if you modify the code like this;

img_src_list = [
    'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png',
    'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png']
image_link = image_finder[0].find_all('img')[0]['src'].replace('//', 'https://')
if image_link in img_src_list:
    print(x)

Comments

0

You can, and this StackOverflow answer shows you all the (many) ways you can split lines.

But in this case I would do it like this:

found_image = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
link_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
link_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'

if found_image == link_one or found_image == link_two:
    print(x)

Comments

0
url_one = 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Question_book-new.svg/50px-Question_book-new.svg.png'
url_two = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Wiktionary-logo.svg/30px-Wiktionary-logo.svg.png'
imagefinder = image_finder[0].find_all('img')[0]['src'].replace('//','https://')
if imagefinder == url_one or imagefinder == url_two:

 print(x)

Comments

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.