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)

Sina M
  • 84
  • 11

4 Answers4

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)
André Kuljis
  • 90
  • 1
  • 9
  • 1
    Third option (as the number of options grows): `if any(fixedLink == x for x in (option1, option2)):` – chepner Aug 30 '20 at 21:09
  • 1
    Or `if fixedLink in {option1, option2}` for O(1) lookup, although it's not really relevant, not even for more values. – tobias_k Aug 30 '20 at 21:13
  • @chepner Am I missing something or is this just a more complicated (and probably slightly slower) version of `if fixedLink in (option1, option2)`? – tobias_k Aug 30 '20 at 21:14
  • 1
    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. – chepner Aug 30 '20 at 21:17
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)
Ahmed Mamdouh
  • 696
  • 5
  • 12
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)
lionkor
  • 562
  • 2
  • 18
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)
pmoh
  • 81
  • 8