0

How do I solve this pls 🙏

So I studied the filter function and I'm trying to return the FALSE part too. For example def pass_grades(grades): return grades > 50 scores = [10,30,50,70,22, 90] passers = list(filter(pass_grades(scores) print(passers) The output will be all pass grades in a list. But what If I want to return pass grade in one list and also failed grades in another list

1st Jun 2021, 2:12 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
4 Respostas
+ 6
Elijah Nwalozie , this task can be done with filter as you can see in the attached code. instead of using a regular function in filter(), it is easier to use a lambda function to solve this task. https://code.sololearn.com/cgPGjI9UbQfy/?ref=app
1st Jun 2021, 4:57 PM
Lothar
Lothar - avatar
+ 3
`filter` is used to sort out stuffs that doesn't match a criteria from an iterable. I don't think it makes sense to use `filter` to split contents of an iterable. If you want to separate the ones who pass from those who fails, then you can prepare 2 extra `list` objects into which you will append stuffs as you iterate through the <scores> iterable, sorting out the stuffs as you go.
1st Jun 2021, 3:29 PM
Ipang
+ 1
you could use reduce() function from functools module to get both lists at once check... https://code.sololearn.com/cECfhVYWjJ2C/?ref=app if you want to include 50 in result of second list (True == 1), then just change comparison operator to >= instead of > in 'fs' lambda used ('fs' stand for shortcut of 'filter_split') ;) more about reduce(): https://docs.python.org/3/library/functools.html https://thepythonguru.com/python-builtin-functions/reduce/ https://realpython.com/python-reduce-function/ https://www.geeksforgeeks.org/reduce-in-python/
1st Jun 2021, 7:19 PM
visph
visph - avatar
0
Completa el código para iterar a través de la lista grades y mostrar solo los valores mayores a 50 for grade in grades: if grade <= 50: break >=50: print(grade)
19th Aug 2024, 3:28 AM
Ester Noemi Argueta