Categories
list python random

How can I randomly select an item from a list?

2212

How do I retrieve an item at random from the following list?

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

0

    3237

    Use random.choice():

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

    For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():

    import secrets
    
    foo = ['battery', 'correct', 'horse', 'staple']
    print(secrets.choice(foo))
    

    secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:

    import random
    
    secure_random = random.SystemRandom()
    print(secure_random.choice(foo))
    

    3

    • 5

      Does making two consecutive calls of random.choice(foo) return two different results?

      Dec 7, 2018 at 17:13

    • 67

      @EduardoPignatelli Each choice is random, so it can return two different results, but depending on the start seed, it’s not guaranteed. If you want to select n distinct random elements from a list lst, use random.sample(lst, n)

      – Graham

      Dec 23, 2018 at 17:02


    • 10

      on a related note, Standard pseudo-random generators are not suitable for security/cryptographic purposes. ref

      – XoXo

      Jan 30, 2019 at 19:46

    266

    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 = {'a', 'b', 'c', 'd', 'e'}  # a sequence or set will work here.
    num_to_select = 2                           # 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]
    second_random_item = list_of_random_items[1] 
    

    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.

    EDIT: Using Secrets

    As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:

    import secrets                              # imports secure module.
    secure_random = secrets.SystemRandom()      # creates a secure random object.
    group_of_items = {'a', 'b', 'c', 'd', 'e'}  # a sequence or set will work here.
    num_to_select = 2                           # set the number to select here.
    list_of_random_items = secure_random.sample(group_of_items, num_to_select)
    first_random_item = list_of_random_items[0]
    second_random_item = list_of_random_items[1]
    

    EDIT: Pythonic One-Liner

    If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.

    import random
    first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
    

    1

    183

    If you also need the index, use random.randrange

    from random import randrange
    random_index = randrange(len(foo))
    print(foo[random_index])
    

    0