#!/usr/bin/env python3 # https://www.geeksforgeeks.org/python/how-to-yield-values-from-list-in-python/ import random players = ['john', 'yaya', 'elise', 'niko'] actions = ['jump', 'walk', 'run', 'swim'] def gen_event(players, actions): player = random.choice(players) action = random.choice(actions) yield player, action # print 1,000 events for event in range(1000): # gen_event() returns a 'generator' g = gen_event(players, actions) # create a tuple event_tuple = tuple(next(g)) # unpack the tuple (player, action) = event_tuple print(f"Event {event}: Player {player} did {action}") print() # create a list of 10 tuples events = [] for i in range(10): g = gen_event(players, actions) event_tuple = tuple(next(g)) events.append(event_tuple) print(f"Build a list of 10 events: {events}") print() # consume list def consume_event(events, nb_events): for i in range(nb_events): ran = random.choice(events) events.remove(ran) yield ran nb_events = len(events) for event in consume_event(events, nb_events): print(f"Got event from list: {event}") print(f"Remains in list: {events}")