99darshan
99darshan

Reputation: 572

Why does string.split('\n') adds an extra element in the output list?

I have a list animals as below

animals = ['pets','dog\ncat\nchicken\ncow\n','wild','tiger\nlion\nrhino\n']

I want a separate list only for pets

pets = animals[1].split('\n')

print(pets) outputs ['dog', 'cat', 'chicken', 'cow', ''] The last element of the pets list i.e '' is undesirable

The line of code below removes the '' element from the list pets

pets = [i for i in animals[1].split('\n') if i]

Could you please explain why the former solution inserts '' in the list. Also, please clarify what is going on inside the list comprehension in the latter solution? what does "if i" represent?

Upvotes: 4

Views: 2616

Answers (2)

Alexander Simko
Alexander Simko

Reputation: 183

The list comprehension iterates over every item in animals[1].split('\n') and then adds the item in a list if the value of bool(i) == True i.e. if i is not empty. This list comprehension is equivalent with:

pets = []
for i in animals[1].split('\n'):
    if i: # checks if i is not empty
        pets.append(i)

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107347

Because always there is an empty string at the leading and trailing of a string (and between all the characters) :

>>> s='example'
>>> s.replace('','*')
'*e*x*a*m*p*l*e*'

And in this case you have a new line character at the trailing (actually before trailing empty string ) so after split you'll have a splitted empty string!

>>> s='dog\ncat\nchicken\ncow\n'
                               ^ #here is the empty string 
>>> s.split('\n')
['dog', 'cat', 'chicken', 'cow', '']

Upvotes: 2

Related Questions