Choosing a random item from List in Python

Python Script to choose a random item from List

Source code :

from random import randint

myList = [10,20,30,40,50,60,70,80]

totalItems = len(myList) - 1

randomIndex = randint(0, totalItems)

print(myList[randomIndex])

Output :

60

Algorithm :

  1. Import therandint function from the random package
  2. Create a List mylist
  3. Find the size of List and assign it to a variable totalItems
  4. Generate a random number ranging from 0 to the size of the list totalitems and assign it to the variable randomIndex
  5. Pick the item from the List based on the index value stored in randomIndex

Output :


                

Comments :