I want to create a list of lists from a list of multi-field strings and wonder if it is possible to do so in a comprehension.
inputs = ["1, foo, bar", "2, tom, jerry"]
[[1, "foo", "bar"], [2, "tom", "jerry"]]
>>> [s.split(",") for s in inputs]
[['1', ' foo', ' bar'], ['2', ' tom', ' jerry']]
[[int(x), y.strip(), z.strip() for x,y,z = s.split(",")] for s in inputs]
[[int(v), v.strip(), v.strip() for v = s.split(",")] for s in inputs]
outputs = 
for s in inputs:
x,y,z = s.split(",")
outputs.append([int(x), y.strip(), z.strip()])
You can do this with two
for clauses in your list comprehension. The first iterates over the items in the list. The second iterates over a single-item list containing the list derived from splitting the string (which is needed so we can unpack this into three separate variables).
[[int(x), y.strip(), z.strip()] for s in inputs for (x, y, z) in [s.split(",")]]
for clauses go in a somewhat counterintuitive order, but it matches the way you'd write it as nested
Jon Sharpe's use of a nested comprehension (generator expression, actually) is similar and probably clearer. The use of multiple
for clauses always seems confusing to me; mainly I wanted to see if I could do make use of it here.