Take the 2-minute tour ×

This question already has an answer here:

I have a list of strings returned after command execution, split on '\n'.

listname = output.decode('utf8').rstrip().split('\n')

When I print using print(listname), I get


Clearly It's a list containing empty string

Because of this I am getting len(listname) as 1.

How to remove this empty string

share|improve this question

marked as duplicate by Aya, askewchan, Spudley, bahrep, dragon112 May 15 '13 at 12:12


You're looking for splitlines. –  gdbdmdb May 14 '13 at 15:29
add comment

3 Answers

up vote 4 down vote accepted

I think this is what you are looking for:


In details:

>>> filter(None, ["Ford", "Nissan", ""])
['Ford', 'Nissan']

P.S. In python 3+ filter returns iterator, so use list(filter(..)).

share|improve this answer
+1 but I had to use list(filter(None,...)) again –  daa May 14 '13 at 14:20
@Drt Good point, updated my answer. –  Alexey Kachayev May 14 '13 at 14:22
add comment
listname = [item for item in output.decode('utf8').rstrip().split('\n') if item]
share|improve this answer
add comment
output = output.decode('utf8').rstrip()
if output:
    listname = []
    listname = output.split('\n')
share|improve this answer
+1, although I'd use a conditional: lst = out.split('\n') if out else [] –  gdbdmdb May 14 '13 at 15:26
add comment
