In this article, I will let you know how to select a random item from a list and other sequence types in Python. Python **random module has an inbuilt function choice() to randomly select an item from a list** and other sequence types.

**Goals of this lesson. **In this lesson, you’ll learn how to:

- Randomly select an item from a list, Set, and Dictionary
- Randomly select multiple choices
- Choose the same element from the list every time
- Choose a random element from a multidimensional array

Recommended Exercise and Quiz:

**Further Reading**: A Detailed Tutorial on **Generate Random Data In Python (11 Articles)**

## Python random.choice() function

Python random module‘s `random.choice()`

function returns a random element from the non-empty sequence. we can use the choice() function for selecting a random password from word-list, Selecting a random item from the available data.

**Syntax of random.choice()**

random.choice(sequence)

Here **sequence can be a list, string, tuple.**

**Return Value**: -This function **returns a single item from the sequence**. If we pass an empty list or sequence to `random.choice ()`

function it will **raise IndexError: **Cannot choose from an empty sequence.

### random.choice() Examples

Now let see how to use `random.choice()`

with the example program.

import random numberList = [111,222,333,444,555] print("random item from list is: ", random.choice(numberList))

**Output**: Run Online

Random item from list is: 333

## Use random.choice() to Randomly select an item from a list

Let assume you have the following movies list and you want to pick one movie from it randomly.

movie_list = ['The Godfather', 'The Wizard of Oz', 'Citizen Kane', 'The Shawshank Redemption', 'Pulp Fiction']

In this example, we are using a `random.choice()`

to select an item from the above list randomly.

import random movie_list = ['The Godfather', 'The Wizard of Oz', 'Citizen Kane', 'The Shawshank Redemption', 'Pulp Fiction'] moview_item = random.choice(movie_list) print ("Randomly selected item from list is - ", moview_item) moview_item = random.choice(movie_list) print ("\n Randomly selected item from list is - ", moview_item)

**Output**: Run Online

Randomly selected item from a list using random.choice is - The Shawshank Redemption Randomly selected item from a list using random.choice is - The Godfather

As you can see we executed `random.choice()`

function two times and every time we got a different item from a list. Also, there are other ways to randomly select an item from a list lets see those now.

### Randomly select multiple choices from the list

As you know, the `random.choice ()`

function only returns a single item from a list. If you want to randomly select more than one item from a list or set, I’d recommend using a random.choices() or random.sample () instead.

**Example**:

import random #sampling with replacement list = [20, 30, 40, 50 ,60, 70, 80] sampling = random.choices(list, k=4) print("Randomly selected multiple choices using random.choices() ", sampling)

**Output**: Run Online

Randomly selected multiple choices using random.choices() [80, 50, 40, 60, 40]

- Use the
**random.sample**function when you want to choose multiple random items from a list without including the duplicates. - Use
**random.choices**function when you want to choose multiple items out of a list including repeated.

## The random choice from Set in Python

If we pass Set object directly to the choice function we will get the TypeError: ‘set’ object does not support indexing.

**Using the random.choice function we can’t choose random items directly from a set, without having to copy it into a tuple**. Let see how to use a choice function to select a random item from Set.

import random weight_set = {20, 35, 45, 65, 82} weight = random.choice(tuple(weight_set)) print ("Randomly item from Set is - ", weight)

**Output**: Run Online

Randomly item from Set is - 65

## The random choice from Dictionary

Let see how to use the `random.choice`

function to select random key and value pair from the dictionary. The random module’s choice function doesn’t accept a dictionary, you need to convert it into a list before passing. Let see the example now.

import random weight_dict = { "Kelly": 50, "Red": 68, "Jhon": 70, "Emma" :40 } key = random.choice(list(weight_dict)) print ("Random key value pair from dictonary is ", key, " - ", weight_dict[key])

**Output**: Run Online

Random key value pair from dictonary is Jhon - 70

## Randomly choose an item from a list along with its index position

Many times we need an index position of an item along with its value. You can accomplish this using a random.randrange() function. So let see how to randomly choose an item from a list along with its index position.

**Example**: –

from random import randrange movie_list = ['The Godfather', 'The Wizard of Oz', 'Citizen Kane', 'The Shawshank Redemption', 'Pulp Fiction'] random_index = randrange(len(movie_list)) item = movie_list[random_index] print ("Randomly selected item and its index is - ", item, "Index - ", random_index)

**Output**: Run Online

Randomly selected item from list using randrange- The Wizard of Oz Inex - 1

## Choose the same element from the list every time

Choosing the same element out of a list is possible. We need to use **random.seed()** and `random.choice`

function together to produces the same element every time. Let see this with an example.

import random float_list = [22.5, 45.5, 88.5, 92.5, 55.4] random.seed(4) random_item = random.choice(float_list) print("random item", random_item) random.seed(4) random_item = random.choice(float_list) print("random item", random_item)

**Output**: Run Online

random item 45.5 random item 45.5

To choose the same element every time, you need to find seed root to a number. Refer to this article to find the seed root of a number.

## Choose a random element from a multidimensional array in Python

Most of the time we work with 2-d or 3-d arrays in Python. Let assume you want to pick a random element from it then how to do it? Let see this with an example. In this example, We used `numpy.random.choice()`

function to pick a random element from the multidimensional array.

import numpy array = numpy.array([[11 ,22, 33], [44, 55, 66], [77, 88, 99]]) print("Printing 2D Array") print(array) print("Choose random row from 2D array") randomRow = numpy.random.randint(3, size=1) print(array[randomRow,:])

Output:

Printing 2D Array [[11 22 33] [44 55 66] [77 88 99]] Choose random row from 2D array [[44 55 66]]

Note: The **output generated by random.choice() function is not cryptographically secure.** To cryptographically secure random choice we need to use

`random.SystemRandom`

class. Refer to How to secure random data in python for more detail.Now, Let see how to cryptographically secure result of a choice function with an example.

import random movie_list = ['The Godfather', 'The Wizard of Oz', 'Citizen Kane', 'The Shawshank Redemption', 'Pulp Fiction'] secure_random = random.SystemRandom() item = secure_random.choice(movie_list) print ("cryptographically secure random choice - ", item)

**Output**: Run Online

cryptographically secure random choice - The Godfather

## Next Steps

To practice what you learned in this article, I have created a Python random data generation Quiz and Exercise project.

- Solve our
**Python Random data generation Quiz**to test your random data generation concepts. - Solve the
**Free Random data generation Exercise**to practice and master the random data generation techniques in Python.

Let me know your comments and feedback in the section below.