w3resource

Python Math: Randomly select an item from a list


55. Random List Item Selector

Write a Python program to randomly select an item from a list.

Sample Solution:

Python Code:

import random
color = ['Red', 'Green', 'Black', 'Orange', 'Black']
print(random.choice(color))

Sample Output:

Black

Pictorial Presentation:

Python Math: Randomly select an item from a list

Flowchart:

Flowchart: Randomly select an item from a list

For more Practice: Solve these Related Problems:

  • Write a Python program to randomly select an item from a list of colors using random.choice() and print the selected color.
  • Write a Python function that accepts a list of fruits and returns one randomly selected fruit.
  • Write a Python script to pick a random element from a list and then remove that element from the list, printing the updated list.
  • Write a Python program to randomly choose an element from a list and check if it matches a given target value.

Go to:


Previous: Write a Python program to print a random sample of words from the system dictionary.
Next: Write a Python program to calculate the absolute value of a floating point number.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.