Python slice first and last element in list

后端 未结 17 1437
有刺的猬
有刺的猬 2020-12-02 17:54

Is there a way to slice only the first and last item in a list?

For example; If this is my list:

>>> some_list
[\'1\', \'B\', \'3\', \'D\',          


        
17条回答
  •  情深已故
    2020-12-02 18:31

    Another python3 solution uses tuple unpacking with the "*" character:

    first, *_, last = range(1, 10)
    

提交回复
热议问题