Skip to content
Related Articles

Related Articles

Improve Article

Python | Remove random element from list

  • Last Updated : 27 Sep, 2019

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()
# 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]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :