How to count similar domains in emails and print every domain only once[python]?
Question: I have dataset of 10 hotmail emails, 4 gmails, 3 mail.com. I want to analyze
list of emails and print how many of each domain(hotmail,gmail etc) is there
and print out. But i do it in a very bruteforce way. I know python has elegant
short code(e.g. itertools, islice, xrange)

hotmail: 10 gmail: 4 mail.com: 3

but i get this:

hotmail 10 hotmail 10 ... hotmail 10 gmail 4 gmail 4 gmail 4 gmail 4 etc

    
    
    def count_domains( emails):
    
        for email in emails:
    
            current_email = email.split("@", 2)[1] # splits at @, john@mail.com => mail.com, 
                                                   #2nd index in the list
            print(current_email)
            current_domain_counter = 0
            for email2 in emails:
                if current_email == email2.split("@",2)[1]:
                    current_domain_counter = current_domain_counter + 1
            #print(current_email current_domain_counter)
            print(current_domain_counter)
    


Answer: You could use collections.Counter:

    
    
    email=['me@mail.com','you@mail.com',"me@gmail.com","you@gmail.com","them@gmail.com",'you@hotmail.com',"me@hotmail.com","you@hotmail.com","them@hotmail.com"]
    
    
    from collections import Counter 
    def count_domains(emails):
        c = Counter()
        for email in emails:
            current_email = email.split("@", 2)[1] # splits at @, john@mail.com => mail.com, 
            c.update([current_email]) # wrap in list or will end up counting each letter                                     #2nd index in the list
        print(c.most_common()) # print most common domains
        print ("gmail.com count = {}".format(c["gmail.com"]))
        print ("mail.com count = {}".format(c["mail.com"]))
        print ("hotmail.com count = {}".format(c["hotmail.com"]))
    
    print count_domains(email)
    
    [('hotmail.com', 4), ('gmail.com', 3), ('mail.com', 2)]
    gmail.com count = 3
    mail.com count = 2
    hotmail.com count = 4
    

