Python | Remove random element from list
Last Updated :
13 Apr, 2023
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
# 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))
OutputThe original list : [6, 4, 8, 9, 10]
List after removal of random number : [6, 4, 8, 9]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Another approach to remove a random element from a list in Python is using the random.choice() method. This method returns a random element from a given list. After getting the random element, it can be removed from the list using the list.remove() method.
Python3
import random
test_list = [6, 4, 8, 9, 10]
print("The original list : " + str(test_list))
# remove a random element from list
random_element = random.choice(test_list)
test_list.remove(random_element)
# Printing result
print("List after removal of random number : " + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [6, 4, 8, 9, 10]
List after removal of random number : [4, 8, 9, 10]
Time complexity: O(n)
Auxiliary Space: O(1)
Method : Using filter () and lambda():
Python3
# import the random library
import random
# Initialize the list
test_list = [6, 4, 8, 9, 10]
# Print the original list
print("The original list : " + str(test_list))
# Use the random.choice() function to randomly select an element from the list
random_element = random.choice(test_list)
# Use the filter function and a lambda function to remove the selected element from the list
test_list = list(filter(lambda x: x != random_element, test_list))
# Print the list after removal of the random element
print("List after removal of random number : " + str(test_list))
#This code is contributed by pinjala Jyothi
OutputThe original list : [6, 4, 8, 9, 10]
List after removal of random number : [4, 8, 9, 10]
Time complexity: O(n)
Auxiliary Space: O(n-1)
Method : Using del keyword
Python3
import random
test_list = [6, 4, 8, 9, 10]
random_element = random.choice(test_list)
# Print the original list
print("The original list : " + str(test_list))
del test_list[test_list.index(random_element)]
print("List after removal of random number :", test_list)
#This code is Contributed by Vinay Pinjala.
OutputThe original list : [6, 4, 8, 9, 10]
List after removal of random number : [6, 4, 8, 9]
Time complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read
Remove an Element from a List by Index in Python We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read
Python - Random Range in List We are given a list we need to find random range in list. For example, a = [10, 20, 30, 40, 50, 60] we need to find random ranges so that given output should be random list and it changes in every execution.Using random.samplerandom.sample can randomly pick elements from the list and preserve origin
2 min read
Python - Remove List Item Removing List Item can be achieved using several built-in methods that provide flexibility in how you remove list items. In this article, we'll explore the different ways to remove list items in Python.Removing Item by Value with remove()The remove() method allows us to remove the first occurrence o
3 min read