zip()
is your friend here:
list1, list2, list3, list4, list5 = zip(
*(line.strip().split('|') for line in open('test.txt')))
As an added bonus, you could also use this even if you didn't know how many columns there were - just assign it to a single variable instead, and you'd get a list, each item of which is the values for a column:
column_values = zip(*(line.strip().split('|') for line in open('test.txt')))
# column_values[0] is [a,a1,a3] ...
Let's step through this a little bit. First, we'll take a look at what happens with just the zip()
bit:
list1, list2, list3, list4, list5 = zip(
[0,1,2,3,4], [0,1,2,3,4], [0,1,2,3,4])
results in list1 = [0,0,0]
and so on, because zip()
takes the first element from each list and puts it in a list as the first element of the result.
Now, how do we get to zip(a,b,c)
from a sequence [a,b,c]
? Simple: we use the *
positional argument expansion operator. zip(*L)
is the same as zip(L[0], L[1], ...)
.
Finally, how do we get the list of lists we need to pass in? We use a generator expression:
(line.strip().split('|') for line in open('test.txt'))
creates a generator that yields a list of the items in each line, one line at a time (and strips whitespace off the items). This is exactly what we need to feed to zip()
to get the result we want.