Skipping every other element after the first

后端 未结 13 2168
深忆病人
深忆病人 2020-12-04 21:13

I have the general idea of how to do this in Java, but I am learning Python and not sure how to do it.

I need to implement a function that returns a list containing

相关标签:
13条回答
  • 2020-12-04 22:07
    def skip_elements(elements):
        # code goes here
        new_list=[]
        for index,alpha in enumerate(elements):
            if index%2==0:
                new_list.append(alpha)
        return new_list
    
    
    print(skip_elements(["a", "b", "c", "d", "e", "f", "g"]))  
    print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']))  
    
    0 讨论(0)
提交回复
热议问题