Problem using Random

Jeremy Whetzel lists at toadmail.com
Sun Oct 28 00:25:50 EDT 2001


Hi,

I'm trying to use random to choose various items from a list.  I'm using
random.choice() to do this, and this works okay, but I've noticed that
this will repeat items already chosen sometimes.  I thought that to
solve this, I would have the code .remove() the item from the list after
having been chosen.  This is more like what I'm wanting to accomplish,
but the problem is that if the list is 10 items long, it will only
return 5 random.choices() before quitting.  Here's the code I'm playing
around with.


mylist = ['a','b','c','d','e','f']
print mylist
for i in mylist:
  c = random.choice(mylist)
  print c
  mylist.remove(c)
  print mylist  ## Just to check the list status after the remove()


Is there any way that I can get the random.choice() to go through and
exhaust all possible choices before quitting?

Thanks for your help,
Jeremy



More information about the Python-list mailing list