469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

How to pop random item from a list?

Hi,

It's been a while since I've played with python.

My question is... whats the best way to pop a random item from a list??

-Thanks

Mar 10 '06 #1
5 16300
On Thu, 2006-03-09 at 21:59 -0800, flamesrock wrote:
Hi,

It's been a while since I've played with python.

My question is... whats the best way to pop a random item from a list??


import random
# ...

item = mylist.pop(random.randint(0,len(mylist)))
Mar 10 '06 #2
flamesrock wrote:
whats the best way to pop a random item from a list??


import random
def popchoice(seq):
# raises IndexError if seq is empty
return seq.pop(random.randrange(len(seq)))

--Ben

Mar 10 '06 #3
marduk wrote:
item = mylist.pop(random.randint(0,len(mylist)))


This is broken because randint(a, b) may return b.
I prefer randrange(len(mylist)) over randint(0, len(mylist)-1) as a fix.

Peter

Mar 10 '06 #4
Thanks guys! :D

Mar 10 '06 #5

On Mar 11, 2006, at 11:21 AM, Peter Otten wrote:
Am Freitag, 10. März 2006 19:38 schrieben Sie:
item = mylist.pop(random.randint(0,len(mylist)))

This is broken because randint(a, b) may return b.
I prefer randrange(len(mylist)) over randint(0, len(mylist)-1) as
a fix.


This brings up an interesting proposal.
random.choice(seq) brings back a random element from a list, why not
add an optional second argument which is a flag to pop the element
instead of choosing?

ie.
> import random
> def choice(seq, pop=False):


... if not pop:
... return seq[random.randrange(len(seq))]
... else:
... return seq.pop(random.randrange(len(seq)))
...
> x = [1, 2, 3]
> choice(x)


1
> x


[1, 2, 3]
> choice(x, True)


1
> x


[2, 3]


[The main reason I am answering your mail is because you may have
intended to
post on c.l.py]

Regarding your enhancement, I don't see any use cases that aren't
handled by
random.sample() already.

Regards,
Peter


I can see a use case. Think of a bag datastructure. You push things
into some container
and pop them out randomly. If random.choice was capable of 'pop' it
would be
implemented implicitly.

random.sample, select elements from a list, but the original list
remains intact. This would
not be the desired 'bag' behavior.
---
Andrew Gwozdziewycz
ap****@gmail.com
http://ihadagreatview.org
http://and.rovir.us
Mar 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bart Nessux | last post: by
4 posts views Thread by Jesse Noller | last post: by
reply views Thread by mj.clift | last post: by
19 posts views Thread by Boris Borcic | last post: by
2 posts views Thread by Brendon Towle | last post: by
13 posts views Thread by Bruza | last post: by
15 posts views Thread by caca | last post: by
reply views Thread by Edwin.Madari | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.