It is generaly a bad idea to shadow a builtin (like
list) by using a variable named after it.You can use slices and array extension to simplify a bit your algorithm:
def unpack(n, lst): result = lst[:n] return result + [None] * (n - len(result))You can use the
itertoolsmodule to improve memory management and allow for any iterable:import itertools def unpack(n, iterable): infinite = itertools.chain(iterable, itertools.repeat(None)) return itertools.islice(infinite, n)Python 3 has an extended unpacking capability that is closer to your needs:
>>> x, y, *z = [1, 2)2] >>> print(x, y, z) 1, 2, []