Jump to content

Python Randomly Select From List

Linux Hint

Recommended Posts

Generating random values or selecting random items from a collection is an essential task in programming. This can be useful for creating simulations, games, tests, or any other scenario where randomness is needed. In Python, there are several ways to randomly select from a list such as using the “random.choices()”, or “random.sample()” functions, etc.

How to Randomly Select/Choose From a List in Python?

The below methods are utilized in Python to randomly select an item/element from a list:

Method 1: Randomly Select From a List Using the “random.choice()” Function

In Python, the “random.choice()” function is used to select or return a random item/element from a non-empty sequence value.



In the above syntax, “seq” indicates any sequence value such as list, tuple, or str.


The following code is used to select a random item/element from the initialized list:

import random
list1 = [45, 66, 88, 19, 32]

In the above code, the “random.choice()” function takes the defined list as an argument and retrieves the random list item.



The random element has been selected from the input list.

Method 2: Randomly Select From a List Using the “random.randrange()” Method

The “randrange()” method of the “random” module is utilized to select a random item/element from the specified range. This can be employed to randomly select from a list.


random.randrange(x, y, z)


  • The “x” parameter specifies the start value.
  • The “y” parameter indicates the end/last value of the specified range.
  • The “z” parameter indicates the step or increment value between the specified range.


The below code is utilized to randomly select from the input list:

import random
list1 = [45, 66, 88, 19, 32]
random_index = random.randrange(len(list1))

According to the above code, the “random.randrange()” method takes the total list length and selects a random integer value based on that. The fetched random integer value is used inside the square notation to return the corresponding element value from the list.



We have successfully selected a random element.

Method 3: Randomly Select From a List Using the “random.sample()” Method

In Python, the “random.sample()” method retrieves the specified length of a random item from the input sequence such as a list.


random.sample(seq, x)

Here, “seq” denotes the sequence such as list, tuple, etc. and “x” denotes the sample length as an integer value.


In this code the “random.sample()” method is utilized to randomly select from the input list:

import random
list1 = [45, 66, 88, 19, 32]
print('Select "1" Random Element: ', random.sample(list1, 1))
print('Select "3" Random Element: ', random.sample(list1, 3))

In these code lines, the “random.sample()” method takes the defined list named “list1” and a number of elements to be selected from the list as its arguments, respectively. The method then retrieves a particular number of random elements/items from the list.



The single and multiple random elements have been selected accordingly.

Method 4: Randomly Select From a List Using the “random.shuffle()” Function

The “random.shuffle()” function is utilized to shuffle the input sequence by reorganizing the element. This method can be utilized to randomly select items from the list.




Go through the below-given example code:

import random
list1 = [45, 66, 88, 19, 32]

In the above example, the “random.shuffle()” function takes the input list and re-organizes it via shuffling. After shuffling, the “square bracket” notation is used to access the first element from the list that becomes random after shuffling.



As seen, the random list element is retrieved against the first index.


The “random.choice()”, “random.randrange()”, “random.sample()”, or the “random.shuffle()” method is used to randomly select an item from the list in Python. This tutorial presented various ways to randomly select an item/element from the list using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...