Slicing out a specific from a list

后端 未结 2 1387
逝去的感伤
逝去的感伤 2021-01-26 11:58

I have one list and I want to print out all elements of it but skip one specific.

a = [1,2,3,4,5,6,7,8,9]

I want to print out:

1,3         


        
2条回答
  •  梦谈多话
    2021-01-26 12:54

    1. If you specify the element to be skipped by its position (index):

      for position, element in enumerate(a):
          if position != specified_position:
              print(element)
      
    2. If you specify the element to be skipped by its value:

      for element in a:
          if element != specified_value:
              print(element)
      

提交回复
热议问题