Remove list element without mutation

后端 未结 6 1378
无人及你
无人及你 2021-02-04 23:36

Assume you have a list

>>> m = [\'a\',\'b\',\'c\']

I\'d like to make a new list n that has everything except for a given

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 00:19

    You can create a new list without the offending element with a list-comprehension. This will preserve the value of the original list.

    l = ['a', 'b', 'c']
    [s for s in l if s != 'a']
    

提交回复
热议问题