Cory Brown
Cory Brown

Reputation: 63

Separating list items based on condition - Python

So I have a list of urls with varying directory structures; ie:

xyz.com/1/
xyz.com/2/
xyz.com/3/
xyz.com/4/
xyz.com/5/
abc.com/6/
abc.com/7/
abc.com/8/
abc.com/9/
abc.com/10/

I need to iterate through this list and group by tld (top level domain) in python. I am using an open source python library to extract tld in a loop; ie:

for item in list:
    registered_domain = tldextract.extract(item).registered_domain

My question is how to group all the urls with the same base tld into separate lists as I iterate through the list of mixed urls; ie:

Output:
    [xyz.com/1/,xyz.com/2/,xyz.com/3/,xyz.com/4/,xyz.com/5/]
    [abc.com/6/,abc.com/7/,abc.com/8/,abc.com/9/,abc.com/10/]

Upvotes: 2

Views: 89

Answers (1)

jpp
jpp

Reputation: 164693

You can use collections.defaultdict combined with str.split. This will create a dictionary mapping domains to URL.

from collections import defaultdict

L = ['xyz.com/1/', 'xyz.com/2/', 'xyz.com/3/', 'xyz.com/4/', 'xyz.com/5/',
     'abc.com/6/', 'abc.com/7/', 'abc.com/8/', 'abc.com/9/', 'abc.com/10/']

d = defaultdict(list)

for url in L:
    d[url.split('/', 1)[0]].append(url)
    # alternatively:
    # d[tldextract.extract(url).registered_domain].append(url)

Result

print(d)

defaultdict(list,
            {'xyz.com': ['xyz.com/1/', 'xyz.com/2/', 'xyz.com/3/',
                         'xyz.com/4/', 'xyz.com/5/'],
             'abc.com': ['abc.com/6/', 'abc.com/7/', 'abc.com/8/',
                         'abc.com/9/', 'abc.com/10/']})

Upvotes: 3

Related Questions