+ 1

Pls how do i print the output on different lines

I tried using the filter function and i couldn't so i used loops. def results(data): pass_list = [] fail_list = [] for grade in data: if grade >= 50: pass_list.append(grade) else: fail_list.append(grade) return pass_list ,fail_list scores = [20,44,68,100,90,48,66,82,21] print(results(scores)). ACTUAL OUTPUT : ([68, 100, 90, 66, 82], [20, 44, 48, 21]) EXPECTED OUTPUT: ([68, 100, 90, 66, 82], ([20, 44, 48, 21])

1st Jun 2021, 2:23 PM
Nwalozie Elijah
Nwalozie Elijah - avatar
3 odpowiedzi
+ 7
Elijah Nwalozie , a very common task to get the values separated in 2 lists is a list comprehension. it is not too complicated and easy to understand. it works like a for loop together with a conditional expression, and it creates a list for each comprehension : https://code.sololearn.com/c83Q2nQ57W2h/?ref=app
1st Jun 2021, 4:37 PM
Lothar
Lothar - avatar
+ 3
use the unpacking operator *, and the newline separator print (*arr, sep = '\n')
1st Jun 2021, 2:30 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 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:21 PM
visph
visph - avatar