Remove list element without mutation

后端 未结 6 1374
无人及你
无人及你 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:24

    We can do it without using in built remove function and also without creating new list variable

    Code:

    # List m
    m = ['a', 'b', 'c']
    
    # Updated list m, without creating new list variable
    m = [x for x in m if x != a]
    
    print(m)
    

    output

    >>> ['b', 'c']
    

提交回复
热议问题