rpb
rpb

Reputation: 3299

NameError in nested list comprehension with Python

The objective is to convert the following for loop into nested comprehension list.

txt=['-100:200','-15:0','0:15','30:45']


all_t=[]
for t in txt:
    all_t.append([int ( idx ) for idx in t.split(":")])

into

all_t=[int(idx) for idx in t.split(":") for t in txt]

However, the compiler return an error

NameError: name 't' is not defined

Appreciate for any help about this error

Upvotes: 1

Views: 75

Answers (1)

Mick
Mick

Reputation: 796

txt=['-100:200','-15:0','0:15','30:45']
all_t=[[int(idx) for idx in t.split(":")] for t in txt]
print(all_t)

Upvotes: 2

Related Questions