What is indexing in Python? – As simple as it appears, giving a proper explanation of how indexing works in Python may get a little tricky. So grab a seat and follow along with our article to form a deeper understanding of indexing in Python.
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 g behind indexing.
So what are iterables?
It is a special type of object 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 for loops.
Under the hood, what all these iterable items carry are two special 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 sin randomItems: print(item)
Apart from Lists, Strings and Tuples are also iterables in Python. Here’s an example of how you can iterate over Strings.
title = "Lose Yourself" #Looping through each character in the string for char in title: print(char)
L o s e Y o u r s e l f
Now that we have some idea of what Iterables are in Python. How does that relate to Indexing?
What is Indexing in Python?
Indexing in Python is a way to refer the individual items within an iterable by its 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. In fact, many of you should be already familiar to it because of its internet popularity within meme culture.
So, if there are 5 elements present within a list. Then the first element (i.e. the leftmost element) holds the “zeroth” place, followed by the elements in first, second, third, and fourth positions.
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"))
Index of Apple: 0 Index of Banana: 4
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.
In the next section, we finally learn how to use the index() method on iterable objects.
What is the 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.
Python Index Operator Syntax
ObjectName[n] #Where n is just an integer number that represents the position of the element we want to access.
Steps to Use Indexing in Python
Below, we’re gonna find out examples of using indexes within Python.
1. Indexing Strings
greetings = "Hello, World!" print(greetings) #Prints the 0-th element in our string print(greetings) #Prints the 5-th element in our string print(greetings) #Prints the 12-th element in our string
H , !
We can clearly see how our print function accesses different elements within our string object to get the specific characters we want.
2. 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.
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])
Hope you enjoyed our article and learned how to use Indexes in your own code. Happy Coding.