How to randomly select an item from a list?

Ray

Assume I have the following list:

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

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

Pēteris Caune

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))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get an item randomly from a list

From Dev

How to select randomly from a list of string-array

From Dev

how to randomly select a certain number of elements from a list

From Dev

How do I randomly select a variable from a list, and then modify it in python?

From Dev

How select an item from dropdown list in angularjs?

From Dev

Finding which item from list was randomly chosen

From Dev

randomly select a color from a premade list

From Dev

Select Randomly an object from a list (Image)

From Dev

how to hide list when user select item from list?

From Dev

Select specific item from list

From Dev

How to select an item from the dropdown list through selenium webdriver and java

From Dev

How to randomly select from arrayLists and add to an object?

From Dev

How to select edge randomly from graph in R?

From Dev

How to select an integer randomly from a list containing integers as well as other objects?

From Dev

How to select an integer randomly from a list containing integers as well as other objects?

From Dev

How to efficiently randomly select array item without repeats?

From Dev

How to select first item in a list

From Dev

How to select a list item in perl

From Dev

How to randomly call any method from a list

From Dev

How to test that an element is randomly selected from a list?

From Dev

Prolog: Select rule randomly from list and pass parameter

From Dev

Python selenium: Randomly select from a url list textfile

From Dev

Prolog: Select rule randomly from list and pass parameter

From Dev

How to randomly choose an item from an array with a 1000 items?

From Dev

Select last wrong item from the list

From Dev

Remove item from a list on second select

From Dev

Select last wrong item from the list

From Dev

select from list not containing particular item in linq

From Dev

Select item from a list based on an array

Related Related

  1. 1

    Get an item randomly from a list

  2. 2

    How to select randomly from a list of string-array

  3. 3

    how to randomly select a certain number of elements from a list

  4. 4

    How do I randomly select a variable from a list, and then modify it in python?

  5. 5

    How select an item from dropdown list in angularjs?

  6. 6

    Finding which item from list was randomly chosen

  7. 7

    randomly select a color from a premade list

  8. 8

    Select Randomly an object from a list (Image)

  9. 9

    how to hide list when user select item from list?

  10. 10

    Select specific item from list

  11. 11

    How to select an item from the dropdown list through selenium webdriver and java

  12. 12

    How to randomly select from arrayLists and add to an object?

  13. 13

    How to select edge randomly from graph in R?

  14. 14

    How to select an integer randomly from a list containing integers as well as other objects?

  15. 15

    How to select an integer randomly from a list containing integers as well as other objects?

  16. 16

    How to efficiently randomly select array item without repeats?

  17. 17

    How to select first item in a list

  18. 18

    How to select a list item in perl

  19. 19

    How to randomly call any method from a list

  20. 20

    How to test that an element is randomly selected from a list?

  21. 21

    Prolog: Select rule randomly from list and pass parameter

  22. 22

    Python selenium: Randomly select from a url list textfile

  23. 23

    Prolog: Select rule randomly from list and pass parameter

  24. 24

    How to randomly choose an item from an array with a 1000 items?

  25. 25

    Select last wrong item from the list

  26. 26

    Remove item from a list on second select

  27. 27

    Select last wrong item from the list

  28. 28

    select from list not containing particular item in linq

  29. 29

    Select item from a list based on an array

HotTag

Archive