Skip to content

Advanced Array Indexing

Matt Larson edited this page Aug 4, 2020 · 1 revision

Advanced Python Array Indexing

Python has some odd array indexing functionality that works a little differently than array indexing in C#. Here is a nice explination of some of the indexing functionality.

"A colon on the right side of an index means everything after the specified index. A colon on the left side of an index means everything before, but not including, the index. And if we use a negative index, it means get elements from the end, going backwards."

Here are some examples for reference when trying to duplicate the functionality in C#.

Python

x = ['matt', 'anthony', 'brad']
x[:2]

['matt', 'anthony']

x[1:]

['anthony', 'brad']

>>> x[-1]

'brad'

x[-2]

'anthony'

x[-2:]

['anthony', 'brad']

x[-1:]

['brad']

x[:-1]

['matt', 'anthony']

x[:-2]

['matt']

Clone this wiki locally