{"id":17203,"date":"2021-08-26T10:42:53","date_gmt":"2021-08-26T05:12:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=17203"},"modified":"2021-11-22T18:37:10","modified_gmt":"2021-11-22T13:07:10","slug":"python-program-to-delete-random-item-from-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-delete-random-item-from-a-list\/","title":{"rendered":"Python Program to Delete Random Item from a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Items from a List with Specific Length<\/a> As this Random module is one of Python’s predefined modules, its methods return random values.<\/p>\n 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.<\/p>\n The random module in Python is made up of various built-in Methods.<\/p>\n The random module is extremely useful for creating a variety of entertaining games.<\/p>\n choice():<\/strong>\u00a0 choice() is used to select an item at random from a list, tuple, or other collection.<\/p>\n Because the choice() method returns a single element, we will be using it in looping statements.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Delete Random items from a given List.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nRandom Module in python :<\/strong><\/p>\nGiven List = ['apple', 'mango', 'banana', 'orange']<\/pre>\n
The given list after deletion of random item = ['apple', 'banana', 'orange']<\/pre>\n
Given List = ['good', 'morning', 'btechgeeks', 'hello', 'all']<\/pre>\n
The given list after deletion of random item = ['good', 'morning', 'hello', 'all']<\/pre>\n
Program to Delete Random Item from a List<\/h2>\n
\n
Method #1: Using random.choice Method (Static input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = ['apple', 'mango', 'banana', 'orange']\r\n# Apply random.choice() method for the above-given list to get the random item and\r\n# store it in another variable.\r\nrandm_item = random.choice(gvn_lst)\r\n# Remove the above obtained random item from the given list.\r\ngvn_lst.remove(randm_item)\r\n# Print the above-given list after deletion of random item from the list.\r\nprint(\"The given list after deletion of random item =\", gvn_lst)\r\n<\/pre>\n
The given list after deletion of random item = ['apple', 'banana', 'orange']<\/pre>\n
Method #2: Using random.choice Method (User input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the List as user input using list(),map(),input(),and split() functions and \r\n#store it in a variable.\r\ngvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Apply random.choice() method for the above-given list to get the random item and\r\n# store it in another variable.\r\nrandm_item = random.choice(gvn_lst)\r\n# Remove the above obtained random item from the given list.\r\ngvn_lst.remove(randm_item)\r\n# Print the above-given list after deletion of random item from the list.\r\nprint(\"The given list after deletion of random item =\", gvn_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = good morning btechgeeks hello all\r\nThe given list after deletion of random item = ['good', 'morning', 'hello', 'all']<\/pre>\n
\n