Skip to content
Related Articles

Related Articles

Python | Remove random element from list

Improve Article
Save Article
  • Last Updated : 27 Sep, 2019
Improve Article
Save Article

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))

Output :

 
The original list : [6, 4, 8, 9, 10]
List after removal of random number : [6, 4, 8, 10]
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!