Python: Including only the last 7 values in each key
Question: I have a dictionary where each key has multiple values. Would it be possible
to include only the last 7 values for each key, and then do basic arithmetic
with it (ex: addition, subtraction, multiplication, division)?

The end objective is to be able to upload date-specific data and be able to
include only the past week, month, or year.

Any nudges in the right direction are very much appreciated.


Answer: Depending on how the incoming data is organized (already sorted vs. random
order), I'd take a look at
[`collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque)
(which can set a maximum length so newly added items seamlessly push out older
items once it reaches the specified limit) for the already sorted case or
rolling your own solution with the [`heapq`
module](https://docs.python.org/3/library/heapq.html) primitives (initially
using `heapq.heappush`, then switching to `heappushpop` when you reach
capacity) for the unordered input case.

Using a
[`collections.defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict)
with either approach as the underlying storage type would simplify code.

Example with bounded `deque`:

    
    
    from collections import defaultdict, deque
    
    recentdata = defaultdict(lambda: deque(maxlen=7))
    for k, v in mydata:
        recentdata[k].append(v)  # If deque already size 7, first entry added is bumped out
    

or with `heapq`:

    
    
    from collections import defaultdict
    from heapq import heappush, heappushpop
    
    recentdata = defaultdict(list)
    for k, v in mydata:
        kdata = recentdata[k]
        if len(kdata) < 7:
            heappush(kdata, v)     # Grow to max size maintaining heap invariant
        else:
            heappushpop(kdata, v)  # Remain at max size, discarding smallest value (old or new)
    

