convert list into random list within list python
Question: Let us imagine that in python we have list of numbers, like this:

    
    
    [1, 3, 4, 5, 6, 7, 8, 9]
    

What is the simplest possible way to convert this list of numbers into a
random series of lists within lists?

Like this:

    
    
    [[1, 2, 3], [4], 5, 6, [7, 8], 9]
    


Answer: Using
[`random.randint`](http://docs.python.org/3/library/random.html#random.randint):

    
    
    import random
    
    def random_series(lst, size=3):
        start = end = 0
        n = len(lst)
        while end < n:
            end += random.randint(1, size)
            if end - start == 1:
                yield lst[start]
            else:
                yield lst[start:end]
            start = end
    

Example usage:

    
    
    >>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> list(random_series(lst))
    [[1, 2, 3], 4, 5, [6, 7], [8, 9]]
    >>> list(random_series(lst))
    [[1, 2], [3, 4], [5, 6], 7, 8, 9]
    >>> list(random_series(lst))
    [[1, 2], [3, 4, 5], [6, 7], [8, 9]]
    >>> list(random_series(lst))
    [[1, 2, 3], [4, 5, 6], 7, [8, 9]]
    

