I'd like to read a file with the following input:
10
20
30
50
60
70
80
90
100
and generate the following output:
[['10', '20', '30'], ['50','60','70'] ... ]
using list comprehensions and not foor loops. Naturally the problem I'm facing is creating the nested list when a \n
character is detected. Of course 'disclaimer' the code would probably be more readable with for loops!
with open('file.txt', 'r') as f:
result = [line.strip() for line in f.readlines() if line != '\n']
print(result)
//
['10', '20', '30', '50', '60', '70']
// not correct