Python index like MATLAB's end/2


MATLAB has a very convenient syntax for getting half of a list:


The syntax I know for python to do this is


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?

6/10/2012 2:49:01 PM

Accepted Answer

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]
6/9/2012 9:13:55 PM

No, lists in python don't have the concept end( a somewhat similar concept is the index -1).
An easy (but not recommended) solution to your problem is:

l = longnamelist

or to copy/paste the long name... (some editors have a shortcut for copying a word, this makes copy/paste of a long name very easy.)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow