Tomonaga
Tomonaga

Reputation: 167

Split in list comprehension

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

Answers (1)

jalanga
jalanga

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

Related Questions