Python: Want to have something taken out of a list how do I do this in python 3?

Question:

Here is my code if that helps you answer the question

import random

Exlist = ['pl1', 'pl2']
Exvar = random.choice(Exlist)
print(Exvar)
Exlist = Exlist - list(Exvar)

I tried changing Exvar to a list

Answers:

In order to remove something from a list you can either remove() or pop() or possibly reconstruct the list omitting the element that’s not required

Answered By: Pingu

Use remove(). For example,

import random

Exlist = ['pl1', 'pl2']
Exvar = random.choice(Exlist)
print(Exvar)
Exlist.remove(Exvar)
Answered By: meable

You can use remove() to remove item by content example:

Exlist.remove(Exvar)

You can use pop() to remove the last item or pass the index as parameter to remove specific item example:

Exlist.pop()
Exlist.pop(index)

You can also use del keyword to remove item by index example:

del Exlist[index]
Answered By: Benhamadouche Walid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.