Reduce the totality of lists in one application

Looking for shrunk list compression and fewer loops and memory usage, is there a way to reduce two loops to build end paths by turning them into a single comprehesion list?

def build_paths(domains):

    http_paths  = ["http://%s"   % d for d in domains]
    https_paths = ["https://%s" % d for d in domains] 

    paths = []                                         
    paths.extend(http_paths)                           
    paths.extend(https_paths)

    return paths                          

In this case, the expected result is an optimized list count, decreasing of the three lists of links ( http_paths, https_paths, paths) in one single row, for example, the following exemplary structure:

def build_paths(domains):
    return [<reduced list comprehesion> for d in domains]

In both cases, the following test is performed:

domains = ["www.ippssus.com",                      
           "www.example.com",                      
           "www.mararao.com"]                      

print(build_paths(domains))

Expected result, independent of list order:

< ['http://www.ippssus.com', 'http://www.example.com', 'http://www.tetsest.com', 'https://www.ippssus.com', 'https://www.example.com', 'https://www.tetsest.com']
+4
source share
1 answer

Add a second loop:

['%s://%s' % (scheme, domain) for scheme in ('http', 'https') for domain in domains]

URL- http, URL- https, .

+14

Source: https://habr.com/ru/post/1611937/


All Articles