How do I randomly select an item from a list using Python?

Let's say, as an example, I have the following list:

foo = ['a', 'b', 'c', 'd', 'e']

What is the best way to retrieve an item at random from this list?


import random

foo = ['a', 'b', 'c', 'd', 'e']

In case you also need the index:

foo = ['a', 'b', 'c', 'd', 'e']
from random import randrange
random_index = randrange(0,len(foo))
print foo[random_index]

I propose a script for removing randomly picked up items off a list until it is empty:

Maintain a set and remove randomly picked up element (with choice) until list is empty.

import random

while len(s)>0:

Three runs give three different answers:

set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])

set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])

import random
random_item = random.choice(foo)

Bit more pythonic way of doing it

If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.

import random
group_of_items = {1, 2, 3, 4}               # a sequence or set will work here.
num_to_select = 1                           # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]

If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).

Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.

if you need the index just use:

import random
foo = ['a', 'b', 'c', 'd', 'e']
print int(random.random() * len(foo))
print foo[int(random.random() * len(foo))]

random.choice does the same:)

We can also do this using randint.

from random import randint
l= ['a','b','c']

def get_rand_element(l):
    if l:
        return l[randint(0,len(l)-1)]
        return None

This is the code with a variable that defines the random index:

import random

foo = ['a', 'b', 'c', 'd', 'e']
randomindex = random.randint(0,len(foo)-1) 
print (foo[randomindex])
## print (randomindex)

This is the code without the variable:

import random

foo = ['a', 'b', 'c', 'd', 'e']
print (foo[random.randint(0,len(foo)-1)])

And this is the code in the shortest and smartest way to do it:

import random

foo = ['a', 'b', 'c', 'd', 'e']

(python 2.7)

import random
print list[random.randint(0, len(list)-1)]

Need Your Help

Changing boost::scoped_ptr to std::unique_ptr

c++ c++11 boost

I am using boost::scoped_ptr in the code which I would like to replace with std::unique_ptr. I would like to know if there are any disadvantages in moving to std::unique_ptr. Boost is portable across

iPhone - generic UITableView

iphone uitableview generics

I know how to build and display the XML data in UITableView. And let's say I have an UITableView named: "DisplayData"

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.