I have a program where I\'m keeping track of the success of various things using collections.Counter
— each success of a thing increments the corr
You could wrap the iterator in list()
to convert it into a list for random.choice()
:
nextthing = random.choice(list(scoreboard.elements()))
The downside here is that this expands the list in memory, rather than accessing it item-by-item as would normally get with an iterator.
If you wanted to solve this iteratively, this algorithm is probably a good choice.