Home > Back-end >  Python: Want to have something taken out of a list how do I do this in python 3?
Python: Want to have something taken out of a list how do I do this in python 3?

Time:02-04

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

CodePudding user response:

Use remove(). For example,

import random

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

CodePudding user response:

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

CodePudding user response:

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]
  • Related