How to randomly select an item from a tuple in Python?


In this article, we will show you how to randomly select an item from a tuple using python. Below are the various methods in python to accomplish this task:

  • Using random.choice() method

  • Using random.randrange() method

  • Using random.randint() method

  • Using random.random()

  • Using random.sample() method

  • Using random.choices() method

Assume we have taken a tuple containing some elements. We will generate a random element from the given input tuple using different methods as specified above.

Using random.choice() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword to import the random module(used to generate random integers. Because these are pseudo-random numbers, they are not actually random. This module can be used to generate random numbers, and print a random value from a tuple, list or string, etc)

  • Create a tuple and add some dummy data to it.

  • Generate a random item from the tuple using random.choice() method(This function returns a random element from the specified sequence i.e tuple here) by passing the input tuple as an argument to the choice() function.

  • Print the generated random tuple item.

Example

The following program returns a random element from the tuple using random.choice() method –

# importing random module import random # input tuple inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code") # printing the given input tuple print("The given input tuple: ", inputTuple) # generating a random item from the tuple using random.choice() method randomItem = random.choice(inputTuple) print("The generated random tuple item = ", randomItem)

Output

On executing, the above program will generate the following output –

The given input tuple: (30, 10, 'TutorialsPoint', 20, 'python', 'code')
The generated random tuple item = 20

Using random.randrange() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Generate a random index value from the tuple using random.randrange() method(Returns a random number within the specified range) by passing the length of the input tuple as an argument to it using the len() function(The number of items in an object is returned by the len() method)

  • Get the element present at the above index from the tuple and create a variable to store it

Example

The following program returns a random element from the tuple using random.randrange() method –

# importing random module import random # input tuple inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code") # generating a random index value by passing the tuple length to the random.randrange() method randomIndex = random.randrange(len(inputTuple)) # Getting the element present at the above index from the tuple randomItem = inputTuple[randomIndex] print("The generated random tuple item = ", randomItem)

Output

On executing, the above program will generate the following output –

The generated random tuple item = TutorialsPoint

Using random.randint() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Generate a random index value from the tuple using random.randint() method(Returns a random number within the specified range) by passing the length of the input tuple as an argument to it using the len() function(The number of items in an object is returned by the len() method)

  • Get the element present at the above index from the tuple and create a variable to store it.

Example

The following program returns a random element from the tuple using random.randint() method –

# importing random module import random # input tuple inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code") # generating a random index value by passing tuple length as an argument # to the random.randint() function randomIndex = random.randint(0, len(inputTuple)-1) # Getting the element present at the above index from the tuple randomItem = inputTuple[randomIndex] print("The generated random tuple item = ", randomItem)

Output

On executing, the above program will generate the following output –

The generated random tuple item = code

Using random.random()

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Generate a random float number using the random.random() function (Returns a float value at random between 0 and 1) and multiply it with the length of a tuple to get a random index and convert the result into an integer using the int() function(converts to an integer).

  • Get the element present at the above index from the tuple and create a variable to store it.

Example

The following program returns a random element from the tuple using random.random() method –

# importing random module import random # input tuple inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code") # generating a random float number and multiplying it with a length # of the tuple to get a random index and converting it into an integer randomIndex = int(random.random() * len(inputTuple)) # Getting the element present at the above index from the tuple randomItem = inputTuple[randomIndex] print("The generated random tuple item = ", randomItem)

Output

On executing, the above program will generate the following output –

The generated random tuple item = 20

Using random.sample() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Generate the required number of random items from the tuple using random.sample() method by passing the tuple, and the number of random items to be generated as arguments to it.

The random.sample() method returns a list containing a randomly selected number of elements from a sequence.
Syntax:
random.sample(sequence, k)
  • Convert the list to a tuple using the tuple() function and print it.

Example

The following program returns n random elements from the tuple using random.sample() method –

# importing random module import random # input tuple inputTuple = (30, 10, "TutorialsPoint", 20, "python", "code") # generating 3 random items from the tuple using random.sample() method randomItems = random.sample(inputTuple, 3) print("The generated 3 random tuple items = ", tuple(randomItems))

Output

On executing, the above program will generate the following output –

The generated 3 random tuple items = (20, 'TutorialsPoint', 30)

Using random.choices() method

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Generate the required number of random items from the tuple using random.choices() method by passing the tuple, and a number of random items to be generated (k) as arguments to it.

The random module contains the random.choices() method. It is useful to select multiple items from a list or a single item from a specific sequence.
Syntax:
random.choices(sequence, k)
  • Convert the list to a tuple using the tuple() function and print it.

Example

The following program returns n random elements from the tuple using random.sample() method –

import random givenTuple = ("Hello", 10, "TutorialsPoint", 20, "python", "code") # generating 3 random items from a tuple using random.choices() method randomItems = random.choices(givenTuple, k=3) # Converting the random elements list to a tuple print("The generated 3 random tuple items = ", tuple(randomItems))

Output

On executing, the above program will generate the following output –

The generated 3 random tuple items = (10, 'python', 'Hello')

Conclusion

In this article, we learned how to choose an element at random from a Python tuple using six different methods. We also learned how to convert a Python list to a tuple.

Updated on: 28-Oct-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements