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,...
Write a Python program that performs the following list operations. Part A Define a list called...
Write a Python program that performs the following list operations. Part A Define a list called numList with elements. 84, 94, 27, 74, 19, 90, 16, 21, 56, 50, 77, 59, 41, 63, 18, 26, 80, 74, 57, 30, 40, 93, 70, 28, 14, 11, 43,65, 91, 83, 22, 53, 74, 44, 73, 55, 47, 74, 81 Display the followings: All the numbers in numList The number of elements in numList The smallest number in numList The largest number in...
#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
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 please! Given an unsorted list (ls) of values (you do not know the datatype)....
In Python please! Given an unsorted list (ls) of values (you do not know the datatype). Only two values appear in the list but they appear many times and are not sorted. Without using any significant additional space (i.e. you cannot copy the list) sort the elements in linear time going through the list only once. For example: Given the list [‘a’, ‘a’, ‘b’, ‘a’, ‘a’, ‘b’, ‘b’, ‘a’] after the function, the list is [‘a’, ‘a’, ‘a’, ‘a’, ‘a’,...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT