10
A Quick intro to Indexing in Python
Python-2.7
Indexing
Idioms

Intro to Lists

Lists are a ordered collection of objects in python. It's really simple to understand, and we'll quickly go over the basics. You can create a list multiple ways in python

example = [] # empty list
example = list() # empty list
example = [1,2,3] # list with three elements
example = [0, "zero"] # elements can be of mixed types

Indexing

Indexing just means accessing elements.
To access elements in a list, you can use the square bracket notation. There are many methods to access elements in python.

Note

python lists are 0-indexed. So the first element is 0, second is 1, so on. So if the there are n elements in a list, the last element is n-1. Remember this!

Single Element Access

You can access single elements using the name followed by a number in []. Like so:

print example[0] # first element
print example[1] # second element, so on

You can access the elements at the end by adding a minus. One of the reasons I love python!

print example[-1]  # n-th (last) element. Note it is -1 and not -0 (:P). 
print example[-2] # n-1-th element

Slicing

Slicing a list gives us another list, instead of a single element. Slicing is an incredibly useful feature in python, one that you will use a lot!

A slice specifies a start index and an end index, and creates and returns a new list based on the indices. The indices are separated by a colon ':'. Keep in mind that the sub-list returned contains only the elements till (end index - 1). For example

example = [1,2,3,4,5] # create a list
print example[0:5] # Whole list, prints [1,2,3,4,5]
print example[1:5] # prints [2,3,4,5]
print example[4:5] # prints [5]

If you leave out the start index, it's assumed to be zero. If you leave out the end index it's assumed to be the length of the list. So:

example = [1,2,3,4,5]
print example[:5] # treated as example[0:5] -> prints [1,2,3,4,5]
print example[3:] # treated it as example[3:5] -> prints [4,5]
print example[:] # treated as example[0:5] -> prints [1,2,3,4,5]

Note that the last example[:] is often used to create duplicate lists. Remember that new_list = old_list just creates a reference to old_list, but

new_list = old_list[:]

creates a copy of the old_list.

More Indexing Tricks

Let's take a list that contains the first 100 numbers, starting from 0. We can do that using range(100), like so:

my_list = range(100) 
print my_list
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Now let's look into the 'step size'. Step Size specifies with element to pick while indexing. So a step size of 1 tells python to pick every element, a step size of 2 means pick alternate elements, and so on.

The step size is specified after the end-index, preceded by a colon. i.e

my_list[start_index:end_index:step_size]

Of course, if you leave start_index and end_index blank, python assumes its 0 and len(my_list). The default step size is one - it gets all the elements.

If you want only even numbers, the step size can be 2

my_list[::2]

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

You can get even numbers between 4 and 40 using:

my_list[4:40:2] # note that the numbers are indices, it's easy to confuse it with actual values.

Now comes my favorite part (and why python is awesome): You can use negative step sizes, for reversing the list traversal. Let's try a -1 step size and see what happens:

 print my_list[::-1]
>>> [99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

That's fantastic!

Some Useful Idioms

In this section, I'm gonna list a few list indexing idioms that I've found useful.

my_list = range(10)
new_list = my_list[:] # Create a duplicate list
reverse_list = my_list[::-1] # Reverse

That's it!

Thanks for reading this blog post. If you have any feedback / corrections / additions/ rants please let me know in the comments.

Author

Notifications

?