I see another 4-line solution here but I don't think it is efficient. it has to do a sum (which is O(n)) each time calling next.
instead, in my solution I keep a sum variable and every time I just need to do a O(1) update to it.
def __init__(self, size): """ Initialize your data structure here. :type size: int """ self.maxsize = size self.sum = 0 self.window = collections.deque() def next(self, val): """ :type val: int :rtype: float """ self.window.append(val) if len(self.window) < self.maxsize: self.sum += val else: self.sum += val - self.window.popleft() return self.sum/self.maxsize