Delete all emails in the list excluding gmails
I am trying to filter my email list to print with gmails only. The list contains multiple emails, separated by commas, for each person. I want to get each gmail gmail keeping the order of the respective names.
class engineerInfo:
firstName = ""
lastName = ""
email = ""
title = ""
engineers = []
for col in rows:
e = engineerInfo()
e.firstName = col[0]
e.lastName = col[1]
e.email = col[2]
e.title = col[3]
engineers.append(e)
while True:
print("1- Print gmails of software engineers")
choice = int(input("Choose from the menu:"))
if choice == 1:
emailList = []
for i in engineers:
if i.email not in emailList:
emailList.append(i.email)
gmailList = []
for i in emailList:
if i != 'gmail.com':
continue
else:
gmailList.append(i)
print(gmailList)
+3
source to share
1 answer
The check should be .endswith('@gmail.com')
. However, email addresses are not case sensitive like (kudos to @kwhicks ):
gmailList = []
for i in emailList:
if i.lower().endswith('@gmail.com'):
gmailList.append(i)
print(gmailList)
But you're better off using a list view for this (replacing the whole loop for
):
gmailList = [i for i in emailList if i.lower().endswith('@gmail.com')]
+5
source to share