Reputation: 167
How can i write this using list comprehension? I have no idea how to unwrap il list-comprehension.
for line in f:
t1, t2, t3 = line.split(" ")
self._list.append(Time(t1,t2,t3))
i've tried
self._tasks = [Time(line.split(' ')[0], line.split(' ')[1], line.split(' ')[2]) for line in f]
is there any better way?
Upvotes: 4
Views: 1722
Reputation: 1576
[Time(t1, t2, t3) for t1, t2, t3 in (line.split() for line in f.splitlines())]
With or without splitlines
depends of your variable f
Upvotes: 1