What is the best way in Python to automatically extend a list to N
elements, if the list has fewer than N
elements?
That is, let's have I have this string: s = "hello there"
. If I do this:
x, y, z = s.split()
I will get an error, because s.split()
returns a list of two elements, but I'm assigning it to 3 variables. What I want is for z
to be assigned None
.
I know I can do this the hard way:
l = s.split()
while len(l) < 3:
l.append(None)
x, y, z = l
But there has to be something more elegant than this.