|Ned Batchelder : Blog | Code | Text | Site|
How to raffle a book
» Home : Blog : September 2013
I'm an organizer for Boston Python, and we like to raffle off books at events. I've got a technique I like for doing it. Here it is recorded for posterity.
We use meetup.com to handle RSVPs for events. I have a small program that queries the meetup.com API for people who have RSVP'd "yes" to an event. I use it to get a list of names, then I shuffle the names, make an iterator, then pick names off it. I do this on the big screen in an interactive prompt:
>>> import random
Iterables like the list of people's names are usually iterated completely with a for loop or a list comprehension. But you can step them one item at a time if you like. iter() creates an iterator from an iterable (confusing, I know...), then you can use next() to get the next item from the iterable.
There aren't many reasons to use iterators like this, but occasionally it can be useful.
tagged: python» 8 reactions