Question

In: Computer Science

Write a program in python such that There exists a list of emails List Ls =...

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

[email protected]

[email protected]

=================================

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

Solutions

Expert 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...


Related Solutions

Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
In python write a program that first creates a list with the integers 0 through 9...
In python write a program that first creates a list with the integers 0 through 9 and then traverses that list RECURSIVELY (no for/while loops allowed) and prints out the integers on the list. NOTE: creating the list does not have to be done recursively.
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT