If I have a list of card suits in arbitrary order like so:
suits = ["h", "c", "d", "s"]
and I want to return a list without the 'c'
noclubs = ["h", "d", "s"]
is there a simple way to do this?
Use list. pop() to remove the first element from a list. Call list. pop(index) on a list with index as 0 to remove and return the first element.
How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.
The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.
suits = ["h","c", "d", "s"] noclubs = [x for x in suits if x != "c"]
>>> suits = ["h","c", "d", "s"] >>> noclubs = list(suits) >>> noclubs.remove("c") >>> noclubs ['h', 'd', 's']
If you don't need a seperate noclubs
>>> suits = ["h","c", "d", "s"] >>> suits.remove("c")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With