I have to take a large list of words in the form:
['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
and then using the strip function, turn it into:
['this', 'is', 'a', 'list', 'of', 'words']
I thought that what I had written would work, but I keep getting an error saying:
"'list' object has no attribute 'strip'"
Here is the code that I tried:
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())
strip_list
19 times then appending your stripped lines. That code has a very bad smell about it. Also if you got that stuff from a file, you should be stripping it on the way in -- building a large list then bashing it into another large list is not a good idea. Also 2, your code should not depend on knowing the length of the longest word/line. Step back a bit -- what are your trying to achieve? What will you do withstrip_list
? - John Machin