All the other answers, and mostly about understanding lists, are excellent. But just to explain your mistake:
strip_list = [] for lengths in range(1,20): strip_list.append(0) #longest word in the text file is 20 characters long for a in lines: strip_list.append(lines[a].strip())
a is a member of your list, not an index. You can write the following:
[...] for a in lines: strip_list.append(a.strip())
Another important comment: you can create an empty list as follows:
strip_list = [0] * 20
But this is not so useful as .append adds material to your list. In your case, it is impractical to create a list with defaut values, since you will create it for each element when adding separated lines.
So your code should look like this:
strip_list = [] for a in lines: strip_list.append(a.strip())
But, of course, the best of them is the same thing:
stripped = [line.strip() for line in lines]
If you have something more complex than just .strip , put it in a function and do the same. This is the most readable way to work with lists.
JoΓ«l Nov 02 '11 at 17:11 2011-11-02 17:11
source share