Tuples

Tuple Indexing

Introduction

Tuples are basically a data type in python. These tuples are an ordered collection of elements of different data types. Furthermore, we represent them by writing the elements inside the parenthesis separated by commas. Besides, we can also define tuples as lists that we cannot change. Therefore, we can call them immutable tuples. Moreover, we access elements by using the index starting from zero. Let us learn more about tuple indexing.

Tuple Indexing

We can access elements in a tuple in the same way as we do in lists and strings. Hence, we can access elements simply by indexing and slicing. Furthermore, the indexing is simple as in lists, starting from the index zero. Moreover, the number that we write inside the square bracket is the index of the tuple. Let us look at a few examples of accessing the elements in a tuple by using tuple indexing.

tuple indexing

                    

>>>tup1 = (10, 3, 4, 22, 1)

# for accessing the first element of the tuple

>>>tup1[0]

10

# accessing the third element of the tuple

>>>tup1[2]

4

>>>tup1[10]

# gives error as the index is only up to 4

IndexError: tuple index out of range

>>>tup1[1+3]

# the expression inside the square brackets results in an integer index 4. Hence, we get the element at the 4th index.

1

 

Negative Indexing

We can use negative indexes also if we want to print the elements from the end. Furthermore, we can perform negative tuple indexing by writing the index number with a negative sign (-). Hence, this indicates that the compiler starts considering the elements in the reverse order, starting from the last element in the tuple.

For example,

                    

>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)

>>> print(tup[-4])

# prints the fourth element from the last

2

Now, we can use negative indexes in slicing also.

>>> print(tup[-4:-1])

# prints elements from last fourth to last first

(2, 56, 890)

Browse more Topics Under Tuples and its Functions

Tuple index() Method

The tuple index() method helps us to find the index or occurrence of an element in a tuple. This function basically performs two functions:

  • Giving the first occurrence of an element in the tuple.
  • Raising an exception if the element mentioned is not found in the tuple.

Syntax:

tuple.index(value)

Here, value refers to the element whose index or occurrence we want to find. Besides, it is mandatory to mention the value.

Let us look at a few examples.

Example 1: Finding the index of an element

                    

>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)

>>> print(tup.index(45))

>>> print(tup.index(890))

#prints the index of elements 45 and 890

2

7

Example 2:

                    

>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)

>>> print(tup.index(3.2))

# gives an error because the element is not present in the tuple.

ValueError: tuple.index(x): x not in tuple

Example 3:

We can also use the index() method to find the occurrence of an element in a given range. Let us look at a few examples.

                    

>>> tup = (22, 2, 45, 4, 2, 2, 56, 890, 1)

>>> print(tup.index(2, 3))

# This means that the compiler will search the occurrence of element 2 but, after index 3.

4

# It gives 4 the first occurrence of 2 after the index 3 is at index 4.

>>> tup = (22, 2, 45, 4, 2, 2, 56, 890, 1)

>>> print(tup.index(8, 3, 6))

# This means that the compiler will search the occurrence of element 8 but, between the index 3 and 6.

# It gives an error because the element is not present in the tuple in the given range.

ValueError: tuple.index(x): x not in tuple

Frequently Asked Questions (FAQs)

Q1. State true or false:

We mention the index inside the curly brackets.

A1. False, we mention the index inside the square brackets.

Q2. State true or false:

tuple index() method gives all the occurrences of an element.

A2. False, only the first occurrence.

Q3. How can we use the negative index in tuple indexing?

A3. We can use negative tuple indexing to access elements in reverse order.

Q4. The tuple index() method throws which error when the element is not found?

A4. ValueError: tuple.index(x): x not in tuple

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.