Change language

Python | Sort URLs by top-level domain

Given a list of URLs, the challenge is to sort the URLs in the list based on the top-level domain. 
Top Level Domain (TLD) is one of the highest-level domains in the Internet’s hierarchical domain name system. Example — org, com, edu. 
This is mainly used when we need to delete pages and sort the URL by top-level domain. It is widely used in open source projects and serves as a usable snippet.

  Input:  url = ["https://www.isb.edu", "www. google.com "," http://cyware.com "," https://www.gst.in "," https://www.coursera.org "," https://www.create.net " , "https://www.ontariocolleges.ca"]  Output:  [’https://www.ontariocolleges.ca’,’ www.google.com’, ’http:// cyware. com’, ’https://www.isb.edu’,’ https://www.gst.in’, ’https://www.create.net’,’ https://www.coursera.org’ ]  Explanation:  The Tld for the above list is in sorted order [’.ca’,’ .com’, ’.com’,’ .edu’, ’.in’,’ .net’ , ’.org’] 

Here are some ways to accomplish the above task.

Method 1: using sorted
You can separate the input by and then use sorting to sort by TLD.

< / tbody>

# Python code to sort the URL in the list based on the top-level domain.

 
# Url list initialization

Input = [ " https://www.isb.edu " , " www.google.com " , " http:// cyware .com " ,

  " https://www.gst.in " , " https: / /www.coursera. org " ,

  " https://www.create.net " , " https://www.ontariocolleges.ca " ]

  
# Sort function in order

def tld ( Input ):

return Input . split ( ’. ’ ) [ - 1 ]

  
# Using sorting and function call

Output = sorted ( Input , key = tld)

 
# Print output

print ( "Initial list is:" )

print ( Input )

print ( "sorted list according to TLD is " )

print ( Output)

  Initial list is:  [’https://www.isb.edu’,’ www.google. com’, ’http:// cyware.com’,’ https://www.gst.in’, ’https://www.coursera.org’,’ https://www.create.net’, ’ https://www.ontariocolleges.ca’]  Sorted list according to TLD is:  [’https://www.ontariocolleges.ca’,’ www.google.com’, ’http: / / cyware.com’, ’https://www.isb.edu’,’ https://www.gst.in’, ’https://www.create.net’,’ https://www.coursera .org’] 

Method 2: Using a lambda
The shortest and most readable way to sort URLs in a list based on top-level domain — this is using lambda.

# Python code to sort a URL in a list based on the top-level domain.

 
# Url list initialization

Input = [ " https://www.isb.edu " , " www.google.com " , " http://cyware.com " ,

" https://www.gst.in " , " https://www.coursera.org " ,

" https://www.create.net " , " https://www.ontariocolleges.ca " ]

 
# Using lambda and sorting

Output = sorted ( Input , key = lambda x: x.split ( ’.’ ) [ - 1 ])

 
# Print output

print ( "Initial list is:" )

print ( Input )

print ( "sorted list according to TLD is" )

print (Output)

  Initial list is:  [’https://www.isb.edu’, ’www.google.com’,’ http:// cyware.com’, ’https://www.gst.in’,’ https://www.coursera.org’, ’https://www.create .net’, ’https://www.ontariocolleges.ca’]  Sorted list according to TLD is:  [’ https://ww w.ontariocolleges.ca’, ’www.google.com’,’ http:// cyware.com’, ’https://www.isb.edu’,’ https://www.gst.in’, ’ https://www.create.net’, ’https://www.coursera.org’] 

Method 3: use in reverse
Reverse input and breaking it up, then applying sorting to sort the URL by TLD

# Python code to sort a URL in a list based on the top-level domain.

 
# Url list initialization

Input = [ " https://www.isb.edu " , "www.google.com" , " http://cyware.com " ,

" https://www.gst.in " , " https://www.coursera.org " ,

" https://www.create. net " , " https://www.ontariocolleges.ca " ]

 
# Inner function to reverse

def internal (string):

 

return list ( reversed (string.split ( ’.’ )))

 
# Using sorting and calling internals for the reverse

Output = sorted ( Input , key = internal)

  
# Print output

print ( " Initial list is: " )

print ( Input )

print ( "sorted list according to TLD is" )

print (Output)

  Initial list is:  [’https://www.isb.edu’,’ www.google.com’ , ’http:// cyware.com’,’ https://www.gst.in’, ’https://www.coursera.org’,’ https://www.create.net’, ’https://www.ontariocolleges.ca’]  Sorted list according to TLD is:  [’https://www.ontariocolleges.ca’,’ www.google.com’, ’http:// cyware .com’, ’https://www.isb.edu’,’ https://www.gst.in’, ’https://www.create.net’,’ https://www.coursera.org ’] 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically