Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python slice first and last element in list

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', '5', 'F']

I want to do this (obviously [0,-1] is not valid syntax):

>>> first_item, last_item = some_list[0,-1]
>>> print first_item
'1'
>>> print last_item
'F'

Some things I have tried:

In [3]: some_list[::-1]
Out[3]: ['F', '5', 'D', '3', 'B', '1']

In [4]: some_list[-1:1:-1]
Out[4]: ['F', '5', 'D', '3']

In [5]: some_list[0:-1:-1]
Out[5]: []
...
like image 583
chown Avatar asked Aug 31 '12 15:08

chown


People also ask

How do you slice the last element of a list in Python?

Python provides a provision for slicing the lists using the subscript operator. For slicing a list, one needs to provide the starting and ending index in the subscript operator. The end index is exclusive, i.e. the sublist generated by this method includes elements from the start index until the end-1 index.

How do I remove the first and last element in a list?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do you cut the first element of a list in Python?

Method 1: Remove Elements From Lists in Python using pop() This pop() method, i.e removes and prints the ith element from the list. This method is mostly used among the other available options to perform this task. This changes the original list.


3 Answers

One way:

some_list[::len(some_list)-1]

A better way (Doesn't use slicing, but is easier to read):

[some_list[0], some_list[-1]]
like image 103
mgilson Avatar answered Oct 16 '22 15:10

mgilson


Python 3 only answer (that doesn't use slicing or throw away the rest of the list, but might be good enough anyway) is use unpacking generalizations to get first and last separate from the middle:

first, *_, last = some_list

The choice of _ as the catchall for the "rest" of the arguments is arbitrary; they'll be stored in the name _ which is often used as a stand-in for "stuff I don't care about".

Unlike many other solutions, this one will ensure there are at least two elements in the sequence; if there is only one (so first and last would be identical), it will raise an exception (ValueError).

like image 39
ShadowRanger Avatar answered Oct 16 '22 14:10

ShadowRanger


Just thought I'd show how to do this with numpy's fancy indexing:

>>> import numpy
>>> some_list = ['1', 'B', '3', 'D', '5', 'F']
>>> numpy.array(some_list)[[0,-1]]
array(['1', 'F'], 
      dtype='|S1')

Note that it also supports arbitrary index locations, which the [::len(some_list)-1] method would not work for:

>>> numpy.array(some_list)[[0,2,-1]]
array(['1', '3', 'F'], 
      dtype='|S1')

As DSM points out, you can do something similar with itemgetter:

>>> import operator
>>> operator.itemgetter(0, 2, -1)(some_list)
('1', '3', 'F')
like image 21
jterrace Avatar answered Oct 16 '22 14:10

jterrace