Sometimes, while working with Python lists, we can have a problem or part of it, in which we desire to convert a list after deletion of some random element. This can have it’s application in gaming domain or personal projects. Let’s discuss certain way in which this task can be done.
Method : Using randrange() + pop()
In this, we just combine the functionality of above functions into one and achieve this task. The random element is chosen by randrange() and then is accessed and removed from the list using pop()
# Python3 code to demonstrate working of # Remove random element from list # Using randrange() + pop() import random # initializing list test_list = [6, 4, 8, 9, 10] # printing list print("The original list : " + str(test_list)) # Remove random element from list # Using randrange() + pop() test_list.pop(random.randrange(len(test_list))) # Printing result print("List after removal of random number : " + str(test_list)) |
The original list : [6, 4, 8, 9, 10] List after removal of random number : [6, 4, 8, 10]
Recommended Posts:
- random.random() function in Python
- Random sampling in numpy | random() function
- Python | Ways to remove particular list element
- Python - Remove first element of list
- Python | Remove given element from list of lists
- Python | Remove element from given list containing specific digits
- Python | Remove given element from the list
- Python | Remove particular element from tuple list
- Python | Remove duplicates based on Kth element tuple list
- Python - Remove rear element from list
- Remove all the occurrences of an element from a list in Python
- Python - Remove Tuples from the List having every element as None
- Python | Generate random numbers within a given range and store in a list
- Python | Select random value from a list
- Generating random number list in Python
- Python - Get a sorted list of random integers with unique elements
- Python - Generate random number except K in list
- Python | Generate random number except K in list
- Python - N Random Tuples list
- Python - Concatenate Random characters in String List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

