Indexing in Python with Examples

What exactly is indexing in Python? – As simple as it appears, explaining how indexing works in Python can be a little tricky. So take a seat and read this article to gain a better understanding of indexing in Python.

Iterables:

Before we get started with indexing, let’s define iterables and their primary function. Iterable knowledge is essential for getting behind indexing.

So, what exactly are iterables?

In Python, it is a special type of object that may be iterated over. That is, you can traverse through all of the various elements or entities present within the object or entity. It is simple to accomplish using for loops.

All iterable items have two special methods called __iter__() or __getitem__() that implement Sequence Semantics.

Lists, tuples, strings etc are examples of iterables in python.

Example

1)For Lists

Approach:

  • Give the list as static input and store it in a variable.
  • The list is iterable.
  • Iterate in the above list using the for loop.
  • Inside the for loop, print the elements of the given list by printing the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
# List is an iterable
gvn_lst = [1, 2, 3, 4, 5]
# Iterate in the above list using the for loop.
for elemnt in gvn_lst:
    # Inside the for loop, print the element of the given list by printing the
    # iterator value.
    print(elemnt)

Output:

1
2
3
4
5

2)For Strings

Example

Approach:

  • Give the string as static input and store it in a variable.
  • Strings are also examples of an iterable.
  • Iterate in the above string using the for loop.
  • Inside the for loop, print the characters of the given string by printing the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
# Strings are also examples of an iterable.
gvn_str = "Python-programs"
# Iterate in the above string using the for loop.
for chrctr in gvn_str:
    # Inside the for loop, print the characters of the given string by printing the
    # iterator value.
    print(chrctr)

Output:

P
y
t
h
o
n
-
p
r
o
g
r
a
m
s

Now that we know what Iterables are in Python. How does this relate to indexing?

Indexing in Python

Indexing in Python is a method of referring the individual items within an iterable based on their position.

In other words, you can directly access the desired elements within an iterable and perform various operations based on your requirements.

Note: 

Objects in Python are “zero-indexed,” which means the position count starts at zero. A similar pattern is followed by several other programming languages. For example C, C++, java, etc,
So, let’s say there are 6 elements in a list. The first element (i.e. the leftmost element) then takes the “zeroth” position, followed by the elements in the first, second, third, fourth, and fifth positions.

Let  array =     10  11  12  13  14

indexes :          0    1    2   3    4

Example

Approach:

  • Give the list as static input and store it in a variable.
  • Apply index() function to the given list by passing “Python” as an argument to it to get index(position) of “Python”.
  • Store it in another variable.
  • Print the index of “Python”.
  • Similarly, print the index of “welcome”.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "Python", "programs"]
# Apply index() function to the given list by passing "Python" as an argument
# to it to get index(position) of "Python"
# Store it in a variable.
indx = gvn_lst.index("Python")
# Print the index of "Python".
print("The Index of Python = ", indx)
# Similarly, print the index of "welcome".
print("The Index of welcome = ", gvn_lst.index("welcome"))

Output:

The Index of Python =  2
The Index of welcome =  0

When the index() method on a list is invoked with the item name as an argument, the index of a specific item within the list is displayed.

Now, we will see how to use the index() method on iterable objects.

Index Operator in Python

The Python Index Operator is represented by opening and closing square brackets [ ].

Syntax:

objectname[n]

n – It is an Integer. It is representing the position of the element we want to access.

Example

Approach:

  • Give the string as static input and store it in a variable.
  • Pass some random number to the given string to get the element present at the given index number.
  • Store it in another variable.
  • Print the element present at the 4th index.
  • Similarly, try for the other numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "Python-programs"
# Pass some random number to the given string to get the element present
# at the given index number.
# Store it in another variable.
elemnt = gvn_str[4]
# Print the element present at the 4th index.
print(elemnt)
# Similarly, try for the other numbers.
print(gvn_str[6])
print(gvn_str[2])
print(gvn_str[8])

Output:

o
-
t
r

Negative Indexing

We just learnt how to use indexing in Lists and Strings to find specific objects of interest. Although we’ve used a positive integer inside our index operator (the square brackets) in all of our prior examples, this isn’t always necessary.

Negative integers are useful when we want to find the last few items of a list or when we want to index the list from the opposite end. Negative Indexing refers to the process of indexing from the opposite end.

Note: It should be noted that with negative indexing, the last element is represented by -1 rather than -0.

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "Python", "programs"]
# Using negative indexing to print the last item in a given list
print(gvn_lst[-1])
# Printing the first item in a given list
print(gvn_lst[-4])
# Printing the last second item in a given list
print(gvn_lst[-3])

Output:

programs
welcome
to