473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 16804
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
8
by: kp87 | last post by:
I am a little bit stuck .... I want to play a bunch of soundfiles randomly, but i want to give each soundfile a rating (say 0-100) and have the likelihood that the file be chosen be tied to its...
0
by: mj.clift | last post by:
Hi All, Could someone please help me translate my code from Python into C#? I'm a beginner at c# so all help is gratefully appreciated. It starts with a given list (l1) such as . For the...
19
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to ...
2
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
0
by: Edwin.Madari | last post by:
use songs.extend( asongs ) #append is for single item - where ever it mightbe. good luck. Edwin -----Original Message-----
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.