As of reading the following input.txt
file:
1 6 14
1 2 7
1 3 9
2 3 10
2 4 15
3 6 2
3 4 11
4 5 6
5 6 9
I wanted to make a list of lists containing each line as a list of integers
[[1, 6, 14],
[1, 2, 7],
[1, 3, 9],
...
[5, 6, 9]]
I achieved that by doing the following
with open('input.txt', 'r') as f:
# gets lines as list of strings [['1, 2, 3,', ...]]
nodes_list = [n.split() for n in f.readlines()]
# convert strings inside the lists to int's [[1, 2, 3], ...]
nodes_list = [[int(y[0]), int(y[1]), int(y[2])] for y in nodes_list]
Is there any way that I can achieve the same with a single comprehension list, without the need of having two separated ones? If not, maybe some cleaner way of doing it?