Return at least X results from split
Question: [split](http://docs.python.org/library/stdtypes.html#str.split) has a maxsplit
parameter, which is useful when you want _at most_ X results. If there
something similar to return _at least_ X results and populate the rest with
`None`s. I'd like to be able to write

    
    
     a, b, c = 'foo,bar'.magic_split(',', 3)
    

and have `a=foo`, `b=bar` and `c=None`.

Any ideas how to write such a function?

Upd. I ended up with a solution which is a combination of
[this](http://stackoverflow.com/a/9294793/989121) and
[this](http://stackoverflow.com/a/9294073/989121) answers:

    
    
    >>> def just(n, iterable, fill=None):
    ...     return (list(iterable) + [fill] * n)[:n]
    ... 
    >>> just(3, 'foo,bar'.split(','))
    ['foo', 'bar', None]
    


Answer: One way would be:

    
    
    from itertools import chain
    from itertools import repeat
    from itertools import islice 
    
    def magic_split(seq, sep, n, def_value=None):
        return list(islice(chain(seq.split(sep), repeat(def_value)), n))
    

You could just return the return value of `islice` if you don't need the list.

If you don't want the values to be cut off when n is less than number of split
elements in seq, the modification is trivial:

    
    
    def magic_split(seq, sep, n, def_value=None):
        elems = seq.split(sep)
        if len(elems) >= n:
             return elems
    
        return list(islice(chain(elems, repeat(def_value)), n))
    

