Python index like MATLAB's end/2
11,219
There is no specialized syntax. If you need to do it a lot, write a function:
def half_list(l):
return l[:len(l)/2]
Author by
Mike
I am a theoretical astrophysicist -- at least I try to be when I'm not programming.
Updated on June 04, 2022Comments
-
Mike almost 2 years
MATLAB has a very convenient syntax for getting half of a list:
x(1:end/2)
The syntax I know for python to do this is
x[:len(x)/2]
This is fine in this case, because
len(x)
is easy to write. But this syntax becomes more than a pain when the name of the list is long (as they sometimes need to be), and even more so when there is a list of similar long names.I know this is a real shot in the dark, but does python have any syntax option like MATLAB's?