fox fox - 1 year ago 77
Python Question

python: a quick way to return list without a specific element

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

noclubs = ["h", "d", "s"]

is there a simple way to do this?

Answer Source
>>> 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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download