In Python list comprehension is it possible to access the item index?

后端 未结 3 1725
慢半拍i
慢半拍i 2021-01-31 13:15

Consider the following Python code with which I add in a new list2 all the items with indices from 1 to 3 of list1:

for ind, obj in enum         


        
3条回答
  •  鱼传尺愫
    2021-01-31 13:37

    If you use enumerate, you do have access to the index:

    list2 = [x for ind, x in enumerate(list1) if 4>ind>0]
    

提交回复
热议问题