Vermil Silvic
Vermil Silvic

Reputation: 23

Why the split function results the ' '?

k='h==j==u'.split('=',maxsplit=-1)
print(k)

the result is:

['h', '', 'j', '', 'u']

Can anyone explain me the '' in the list? I thought the result will be ['h=','j=','u'] or something doesn't have the ''. I have tried rsplit and the maxsplit number but it's still the same, except for maxsplit=1.

Upvotes: 0

Views: 36

Answers (1)

aloisdg
aloisdg

Reputation: 23521

The empty line '' is the element between = and = in ==.

You can filter it if it bother you:

[x for x in "a=b==c".split('=') if x != '']

If you are looking for only true values:

[x for x in "a=b==c".split('=') if x]

Try it online!

Upvotes: 1

Related Questions