Code Examples

Build email list

programming

This function, written in Python, receives a dictionary, which contains domain names as keys, and a list of users as values.

It generates a list that contains complete email addresses (e.g. diana.prince@gmail.com).

def email_list(domains):
	emails = []
	for domain in domains.keys():
	  for user in domains[domain]:
	    emails.append(user + "@" + domain)
	return(emails)

# Test cases
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"],
                  "yahoo.com": ["barbara.gordon", "jean.grey"], 
                  "hotmail.com": ["bruce.wayne"]}))

# Must return:
# ['clark.kent@gmail.com', 'diana.prince@gmail.com', 'peter.parker@gmail.com',
# 'barbara.gordon@yahoo.com', 'jean.grey@yahoo.com', 'bruce.wayne@hotmail.com']