Houmes
Houmes

Reputation: 71

removing space that turned into string in a list

I have a specific file that I extracted lines from and it had a lot of space in it after put these lines in a list

 lines_list = open('data\wonderland.txt').read().splitlines()

I end up with this result

[' Down the Rabbit-Hole','','','','Alice was beginning to get very tired of sitting by her sister on the','','bank, and of having nothing to do: once or twice she had peeped into the','',

I specifically want to delete the '' from my list

Upvotes: 0

Views: 63

Answers (1)

Pedro Maia
Pedro Maia

Reputation: 2732

Use the built-in filter function:

lines_list = list(filter(None, lines_list))

Output:

[' Down the Rabbit-Hole', 'Alice was beginning to get very tired of sitting by her sister on the', 'bank, and of having nothing to do: once or twice she had peeped into the']

Or just:

lines_list = [i for i in lines_list if i]

Which has the same output

Upvotes: 4

Related Questions