Program to Delete Random Item from a List

Python Program to Delete Random Item from a List

In the previous article, we have discussed Python Program to Print Items from a List with Specific Length
Random Module in python :

As this Random module is one of Python’s predefined modules, its methods return random values.

It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.

The random module in Python is made up of various built-in Methods.

The random module is extremely useful for creating a variety of entertaining games.

choice():  choice() is used to select an item at random from a list, tuple, or other collection.

Because the choice() method returns a single element, we will be using it in looping statements.

Examples:

Example1:

Input:

Given List = ['apple', 'mango', 'banana', 'orange']

Output:

The given list after deletion of random item = ['apple', 'banana', 'orange']

Example2:

Input:

Given List = ['good', 'morning', 'btechgeeks', 'hello', 'all']

Output:

The given list after deletion of random item = ['good', 'morning', 'hello', 'all']

Program to Delete Random Item from a List

Below are the ways to Delete Random items from a given List.

Method #1: Using random.choice Method (Static input)

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in another variable.
  • Apply random. choice() method for the above-given list to get the random item and store it in another variable.
  • Remove the above obtained random item from the given list.
  • Print the above-given list after deletion of random items from the list.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in another variable.
gvn_lst = ['apple', 'mango', 'banana', 'orange']
# Apply random.choice() method for the above-given list to get the random item and
# store it in another variable.
randm_item = random.choice(gvn_lst)
# Remove the above obtained random item from the given list.
gvn_lst.remove(randm_item)
# Print the above-given list after deletion of random item from the list.
print("The given list after deletion of random item =", gvn_lst)

Output:

The given list after deletion of random item = ['apple', 'banana', 'orange']

Method #2: Using random.choice Method (User input)

Approach:

  • Import random module using the import keyword.
  • Give the List as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Apply random. choice() method for the above-given list to get the random item and store it in another variable.
  • Remove the above obtained random item from the given list.
  • Print the above-given list after deletion of random items from the list.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the List as user input using list(),map(),input(),and split() functions and 
#store it in a variable.
gvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split()))
# Apply random.choice() method for the above-given list to get the random item and
# store it in another variable.
randm_item = random.choice(gvn_lst)
# Remove the above obtained random item from the given list.
gvn_lst.remove(randm_item)
# Print the above-given list after deletion of random item from the list.
print("The given list after deletion of random item =", gvn_lst)

Output:

Enter some random List Elements separated by spaces = good morning btechgeeks hello all
The given list after deletion of random item = ['good', 'morning', 'hello', 'all']

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.