re: 30 Days of Python 👨‍💻 - Day 6 - Loops II & Functions VIEW POST


Hey, I came up with this odd solution. It's obviously not as clean as yours but I'm trying to apply some concepts you have explained in the previous articles, so there you go :)

email_list = ['roger@hey.com','michael@hey.com','roger@hey.com','prince@gmail.com']
email_set = set(email_list)
duplicate_dict = dict.fromkeys(email_set, 0)

if (email_list.__len__() > 1):
  for email in email_list:
    if (email in email_set):
      duplicate_dict[email]+= 1

  for key in duplicate_dict.keys():
    if (duplicate_dict[key] > 1):

What do you guys think about it?


You should use len(email_list) instead of _ _ len() _ _ (excuse the spaces, the text goes bold if I leave them out). The _ _ method is really an internal method len() looks for on a class. There are lots of them, actually check out the special methods section the docs for info.


Nice try :) but as Adrian suggested it is not recommended to use dunder methods unless you are planning to override some built-in utility function in a class.

Let me provide an alternate one-line solution using comprehensions which I discovered lately :)

email_list = ['roger@hey.com','michael@hey.com','roger@hey.com','prince@gmail.com']
duplicates = list(set([email for email in email_list if email_list.count(email) > 1 ]))


Comprehensions look way cool but they can impact the code readability.

code of conduct - report abuse