Indexing in Python – A Complete Beginners Guide

Indexing In Python

Data structures in Python include lists, tuples, etc. These data structures can have multiple elements in them, each having some different properties but the problem is how to refer to a particular element from the hundreds of elements they contain. Here indexing comes into action. Indexing is a simple but fundamental concept that is important to learn before further processing with Python data structures.

This tutorial will explain everything you need to know about indexing in Python. But first, let’s take a quick look at iterables.

Prerequisite – What are Iterables?

Before we get started with indexing, let’s understand what iterables are and what is their main function. The knowledge of iterables is much needed to go behind indexing.

Iterables in Python

Iterables are a special type of objects in Python that you can iterate over. Meaning you can traverse through all the different elements or entities contained within the object. It can be easily achieved using the for loops.

Under the hood, what all these iterable items carry are two unique methods called __iter__() or __getitem__() that implement the Sequence Semantics.

#Lists are iterable items in Python. 
randomItems = [4, 6, 2, 56, 23, 623, 453]

#Each individual element inside a list can be accessed using a for loop
for item in randomItems: 
    print(item)

Besides lists in Python, strings and tuples are also iterable.

Example:

title = "Lose Yourself" 

#Looping through each character in the string
for char in title: 
    print(char)

Output:

L
o
s
e

Y
o
u
r
s
e
l
f

Now that we know what Iterables are in Python. How is this related to indexing?

What is Indexing in Python?

Indexing in Python is a way to refer to the individual items within an iterable by their position. In other words, you can directly access your elements of choice within an iterable and do various operations depending on your needs.

Before we get into examples of Indexing in Python, there’s an important thing to Note:

In Python, objects are “zero-indexed” meaning the position count starts at zero. Many other programming languages follow the same pattern. So, if there are 5 elements present within a list. Then the first element (i.e. the leftmost element) holds the “zeroth” position, followed by the elements in the first, second, third, and fourth positions.

Python Index Method

The index of a specific item within a list can be revealed when the index() method is called on the list with the item name passed as an argument.

Syntax:

iterable.index(item)

Where item is an element that index value we want to get.

Example:

fruits = ["apple", "grape", "orange", "guava", "banana"]

#Printing out the indexes of Apples and Banana
print("Index of Apple: ", fruits.index("apple"))
print("Index of Banana: ", fruits.index("banana"))

Output:

Index of Apple: 0
Index of Banana: 4

Python Index Operator

The Python Index Operator is represented by opening and closing square brackets: []. The syntax, however, requires you to put a number inside the brackets.

Syntax:

iterable[n] 

Where n is just an integer number representing the position of the element we want to access.

Example:

greetings = "Hello, World!"

print(greetings[0]) #Prints the 0-th element in our string

print(greetings[5]) #Prints the 5-th element in our string

print(greetings[12]) #Prints the 12-th element in our string

Output:

H
,
!

We can see how our print function accesses different elements within our string object to get the specific characters we want.

Negative Indexing in Python

We’ve recently learned how to use indexing in Lists and Strings to get the specific items of our interest. Although in all our previous cases we’ve used a positive integer inside our index operator (the square brackets), it’s not necessarily needed to be that way.

Often, if we are interested in the last few elements of a list or maybe we just want to index the list from the opposite end, we can use negative integers. This process of indexing from the opposite end is called Negative Indexing.

Note: In negative Indexing, the last element is represented by -1 and not -0.

Example:

letters = ['a', 's', 'd', 'f']

#We want to print the last element of the list
print(letters[-1]) #Notice we didn't use -0 

#To print the 2nd last element from an iterable
print(letters[-2])

Output:

f
d

Conclusion

In this tutorial, we’ve learned that indexing is just a way of referencing the elements of an iterable. We have used the Python index() method to get the index of a particular element. We’ve also looked at the Python index operator and what negative indexing is. Hope you enjoyed the tutorial and learned how to implement indexing in your next project.

Reference

https://docs.python.org/3/tutorial/datastructures.html