import redis redis_cycler_db = redis.Redis(host='localhost', port=6379, db=9) def redis_cycle(list_name): """ Emulates itertools.cycle() but returns the complete shuffled list. :param list_name: :return: """ to_move = redis_cycler_db.rpop(list_name) if not to_move: return [] redis_cycler_db.lpush(list_name, to_move) new_list = redis_cycler_db.lrange(list_name, 0, -1) return [x.decode('utf-8') for x in new_list] def add_backend_cycler(list_name: str, new_elements: list): existing_elements = [i.decode('utf-8') for i in redis_cycler_db.lrange(list_name, 0, -1)] existing_set = set(existing_elements) with redis_cycler_db.pipeline() as pipe: # Add elements for element in new_elements: if element not in existing_set: pipe.rpush(list_name, element) # Remove elements for element in existing_set: if element not in new_elements: pipe.lrem(list_name, 0, element) pipe.execute()