I recommend using a collections.deque
to store a recently watched list. It saves a list like collection of items, and as you add to it, if it gets to its max length, it automatically drops the oldest items, on a first in, first out basis.
import collections
And here's a generator that you can use to get random vids, that haven't been recently watched. The denom argument will allow you to change the length of the recently watched list because it's used to determine the max length of your recently_watched as a fraction of your list of vids.
def gen_random_vid(vids, denom=2):
'''return a random vid id that hasn't been recently watched'''
recently_watched = collections.deque(maxlen=len(vids)//denom)
while True:
selection = random.choice(vids)
if selection not in recently_watched:
yield selection
recently_watched.append(selection)
I'll create a quick list to demo it:
vids = ['vid_' + c for c in 'abcdefghijkl']
And here's the usage:
>>> recently_watched_generator = gen_random_vid(vids)
>>> next(recently_watched_generator)
'vid_e'
>>> next(recently_watched_generator)
'vid_f'
>>> for _ in range(10):
... print(next(recently_watched_generator))
...
vid_g
vid_d
vid_c
vid_f
vid_e
vid_g
vid_a
vid_f
vid_e
vid_c