Why does string.split ('\ n') add an extra item to the output list?
I have a list of animals as shown below
animals = ['pets','dog\ncat\nchicken\ncow\n','wild','tiger\nlion\nrhino\n']
I want a separate list for pets only
pets = animals[1].split('\n')
print (pets) outputs ['dog', 'cat', 'chicken', 'cow', ''] The last item in the list of pets ie '' is not desired
The line of code below removes the item '' from the list of pets
pets = [i for i in animals[1].split('\n') if i]
Could you please explain why the first solution is inserting '' into the list. Also please clarify what's going on inside the list comprehension in the last solution? what does "if i" mean?
source to share
Because there is always a blank line at the beginning and end of the line (and between all characters):
>>> s='example'
>>> s.replace('','*')
'*e*x*a*m*p*l*e*'
And in this case, you have a new line character at the end (actually before the empty empty string), so after splitting, you will have a splayed empty string!
>>> s='dog\ncat\nchicken\ncow\n'
^ #here is the empty string
>>> s.split('\n')
['dog', 'cat', 'chicken', 'cow', '']
source to share
The comprehension of the list iterates over each element in animals[1].split('\n')
, and then adds the element to the list if the value is bool(i) == True
ie, if I'm not empty. This list comprehension is equivalent to:
pets = []
for i in animals[1].split('\n'):
if i: # checks if i is not empty
pets.append(i)
source to share