Slicing every element of a list
Christopher Subich
spam.csubich+block at block.subich.spam.com
Tue Jul 12 16:06:18 EDT 2005
Gary Herron wrote:
> Alex Dempsey wrote:
>> for line in lines:
>> line = line[1:-5]
>> line = line.split('\"\t\"')
> This, in fact, did do the operation you expected, but after creating the
> new value and assigning it to line, you promptly threw it away. (Because
> the loop then went back to the top and (re)assigned the next thing in
> lines to line wiping out your nicely sliced computation in lines.) You
> need to *do* something with the value in line before you end the loop --
> but what?
As an intermediate tip, the entire loop can be written as a single list
comprehension:
stuff = [li[1:-5].split('"\t"') for li in lines]
(You don't need to escape single quotes inside double-quoted strings,
and vice versa.)
More information about the Python-list
mailing list