Skip to main content
added 7 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

how to optimise method with if elif Showing the domain of a URL

I have such a method which takes url f.e. :a URL, such as http://twitter.com/ttt or twitter.com/dasd, and should show its domain with http: http://twitter.com and name twitter

def show_domain(url):
    sites = {
        'twitter': ['http://www.twitter.com/', 'twitter'],
        'facebook': ['http://www.facebook.com/', 'facebook'],
        'pininterest': ['http://www.pininterest.com/', 'pininterest']
    }

    if sites['twitter'][0] in url:
        link = sites['twitter'][0]
        brand = sites['twitter'][1]
    elif sites['facebook'][0] in url:
        link = sites['facebook'][0]
        brand = sites['facebook'][1]

    else:
        link = sites['pininterest'][0]
        brand = sites['pininterest'][1]
    return link, brand

Is there way to optimise this code?

how to optimise method with if elif

I have such method which takes url f.e. : http://twitter.com/ttt or twitter.com/dasd and should show its domain with http: http://twitter.com and name twitter

def show_domain(url):
    sites = {
        'twitter': ['http://www.twitter.com/', 'twitter'],
        'facebook': ['http://www.facebook.com/', 'facebook'],
        'pininterest': ['http://www.pininterest.com/', 'pininterest']
    }

    if sites['twitter'][0] in url:
        link = sites['twitter'][0]
        brand = sites['twitter'][1]
    elif sites['facebook'][0] in url:
        link = sites['facebook'][0]
        brand = sites['facebook'][1]

    else:
        link = sites['pininterest'][0]
        brand = sites['pininterest'][1]
    return link, brand

Is there way to optimise this code?

Showing the domain of a URL

I have such a method which takes a URL, such as http://twitter.com/ttt or twitter.com/dasd, and should show its domain with http: http://twitter.com and name twitter

def show_domain(url):
    sites = {
        'twitter': ['http://www.twitter.com/', 'twitter'],
        'facebook': ['http://www.facebook.com/', 'facebook'],
        'pininterest': ['http://www.pininterest.com/', 'pininterest']
    }

    if sites['twitter'][0] in url:
        link = sites['twitter'][0]
        brand = sites['twitter'][1]
    elif sites['facebook'][0] in url:
        link = sites['facebook'][0]
        brand = sites['facebook'][1]

    else:
        link = sites['pininterest'][0]
        brand = sites['pininterest'][1]
    return link, brand

Is there way to optimise this code?

edited tags
Link
SylvainD
  • 29.3k
  • 1
  • 47
  • 93
Source Link

how to optimise method with if elif

I have such method which takes url f.e. : http://twitter.com/ttt or twitter.com/dasd and should show its domain with http: http://twitter.com and name twitter

def show_domain(url):
    sites = {
        'twitter': ['http://www.twitter.com/', 'twitter'],
        'facebook': ['http://www.facebook.com/', 'facebook'],
        'pininterest': ['http://www.pininterest.com/', 'pininterest']
    }

    if sites['twitter'][0] in url:
        link = sites['twitter'][0]
        brand = sites['twitter'][1]
    elif sites['facebook'][0] in url:
        link = sites['facebook'][0]
        brand = sites['facebook'][1]

    else:
        link = sites['pininterest'][0]
        brand = sites['pininterest'][1]
    return link, brand

Is there way to optimise this code?