1
a = ['google.com', 'bing.com', 'yahoo.co.in']

Output = ['.com', '.in']

How do I get this output without using regular expressions?

Tried using nested for loop and partition function but could not get the output.

Avinash
  • 852
  • 5
  • 18
Ronaldo7
  • 11
  • 1
  • Does this answer your question? [Extract domain from URL in python](https://stackoverflow.com/questions/44113335/extract-domain-from-url-in-python) – 0stone0 Apr 14 '22 at 15:24

4 Answers4

5

Try this:

output = set(f".{w.split('.')[-1]}" for w in a)
Riccardo Bucco
  • 11,231
  • 3
  • 17
  • 42
1

Or

set(["."+ x.rsplit('.', 1)[-1] for x in a])

Or using pop()

set(["."+ x.rsplit('.', 1).pop() for x in a]))
Agaz Wani
  • 5,080
  • 7
  • 41
  • 58
0

It is possible to do this using nested for loop. Try this:

a = ['google.com','bing.com','yahoo.co.in']
output = []
for ele in a:
    count = 0
    for char in reversed(ele):
        count += 1
        if char == '.':
            break
        
    word = ele[-1*count:]
    if word not in output:
        output.append(word) 

print(output)
Avinash
  • 852
  • 5
  • 18
0

you could try:

a = ['google.com', 'bing.com', 'yahoo.co.in']
output = []
for domain in a:
    suffix = '.' + str(domain.split('.')[-1])
    if not suffix in output:
        output.append(suffix)
    
proPhet
  • 1,127
  • 4
  • 13
  • 35