In: Computer Science
Write a program in python such that There exists a list of emails List Ls = ['[email protected]','[email protected]','[email protected]','[email protected]',[email protected]']
Count the number of emails IDS which ends in "ac.in"
Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in
output
2
=================================
i am trying like this but getting confused
len [True for x in Ls if x.endswith('.ac.in')]
please write complete progam and show output and also explain the solution
Please find the updated code,
First of all,
- The syntax of the list comprehension is:
[expression for item in list]
- Then if you want to add to any extra
condition like the filter you can do this:
[expression for item in list if(item) ]
Here, all the items in the list which satisfy the condition will
present in the expression which is mentioned.
- In your case, you mentioned True which is not correct
len [True for x in Ls if x.endswith('.ac.in')]
Use this syntax
[x for x in emailId if x.endswith('.ac.in')]
Please ignore LS and emailId, As both hold the same data.
- After this, the list comprehension will create a new list, so
store it in a new list. After storing calculate the length of the
list and print the emails line by line as:
MailsId =[x for x in emailId if x.endswith('.ac.in')]
print(len(MailsId))
for i in MailsId:
print(i)
Code:
emailId=['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']
#The method that you are doing is related to List Comprehension, which will indeed return a list
#First of all,
#1.create a new list named MailsId which iterates over the list
emailId
#2.Then place the condition that if the element in emailId list
should end with ".ac.in"
#3.If so then fetch the element from the list(Noticed the x after
the "[" You missed this part,this will fetch)
#4.Then after creating a list, calculate the length using
len(MailsId)
#5.Then print out each element in the list one per line using for
loop
MailsId =[x for x in emailId if x.endswith('.ac.in')]
print(len(MailsId))
for i in MailsId:
print(i)
Output:
(Feel free to drop me a comment, If you need any help)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...