• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Randomly Select Item from List in Python? [Choice method]

Aniruddha Chaudhari/147780/2
CodePython

For randomly selecting an item from List in Python, I prefer to go with an example. It makes understanding clear.

Assume we have the following list which contains the name of some all-time greatest cricket players:

player = ['Kohli', 'Tendulkar', 'Lara', 'Ponting', 'Kallis']

So, what is the simplest way to retrieve an item at random from this list?

We are quite confused, and we want to select one random item to give our decision. We can use the random module in python which has function choice() to choose any random item from the list. And another way is by using Numpy Python module.

In this post, I will write the code for both.

1. Randomly Select Item from List in Python Using random.choice() method:

import random
player = ['Kohli', 'Tendulkar', 'ABD', 'Ponting', 'Kallis']
print(random.choice(player))

2. Randomly Select Item from List in Python using Numpy module:

You can use Numpy module as well. It works fine for all Python versions.

Numpy module does not come with Python binaries, you need to install this module using the pip tool.

import numpy
player = ['Kohli', 'Tendulkar', 'ABD', 'Ponting', 'Kallis']
print numpy.random.choice(player)

It will print the name of the player randomly from the list.

Numpy is one of the best modules for data science. And there are very interesting methods to explore in Numpy.

Now finding perk to make the things interesting:

It will be a very useful code; you can use it in many ways. Suppose you are a group of buddies and you want to do some task. It’s obvious no one is willing to do the job if it is tedious.

Here comes the use of this code to randomly select an item from the list in Python. Put all your buddies on the list and run random.choice() function or use Numpy module. Before running, just take a promise to be committed to the result. 😛

Now assign your tedious task to a misfortune guy who has been chosen randomly. 😀

If you have a long list of entries and want to choose a random entry from the file, you can use CSV file format. For more detail read about CSV file and How to read CSV file in Python.

Happy Coding!

Python Interview Questions eBook

Pythonpython list
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Mark Wun
    February 16, 2020 at 6:08 pm

    Thanks for this simple solution. I guess there are some other methods in the list to randomly select the elements. This is easy though.

    • Reply
      Aniruddha Chaudhari
      February 16, 2020 at 6:23 pm

      You are right, Mark!

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard