Since I do not see your exact string, it is difficult to give you a solution that meets your requirements, but if you want to get all the items in the list that are not empty strings, you can do this:
>>> l = ["ch", '', '', 'e', '', 'e', 'se'] >>> [var for var in l if var] Out[4]: ['ch', 'e', 'e', 'se']
You can also use filter with None or bool :
>>> filter(None, l) Out[5]: ['ch', 'e', 'e', 'se'] >>> filter(bool, l) Out[6]: ['ch', 'e', 'e', 'se']
If you want to get rid of lists with empty strings, then for your specific example you can do this:
with open("text.txt", 'r') as file: for line in file: line = line.rstrip('\n' + '').split(':') # If line is just empty if line != ['']: print line
source share