Python program to access front and rear element from tuple

In this program, we are given a tuple with integer elements. We need to create a Python program to access the front and rear elements from the tuple.
Submitted by Shivang Yadav, on December 23, 2021

While working with python programs extracting data from collections to perform a certain operation on the data. Here, we will create a Python program to access front and rear elements from the tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Accessing front and rear elements of tuple

In this article, we will be learning to access front and rear elements of the tuple in Python programming language using different methods.

Input:
(4, 1, 7, 8, 5, 3)

Output:
4, 3

Extracting the first and last, elements can be done easily by getting the value at index 0 and n-1. This can be done in Python using multiple ways.

Method 1:

One method to extract the elements at the first and last index is by using the bracket expression as we extract in a list.

  • The first element extracted as [0]
  • The last element extracted as [-1]
# Python program to access front and rear 
# elements from tuple

# Initializing and printing tuple
intTup = (4, 1, 7, 8, 5, 3)
print("The elements tuple are " + str(intTup))

# Accessing and printing front and 
# rear elements from the tuple 
frontEle = intTup[0] 
rearEle = intTup[-1]

print("The front element of tuple is : " + str(frontEle))
print("The rear element of tuple is : " + str(rearEle))

Output:

The elements tuple are (4, 1, 7, 8, 5, 3)
The front element of tuple is : 4
The rear element of tuple is : 3

Method 2:

Another method to solve the problem is by using Python's itemgetter operation from the operator library. The itemgetter returns the values at the required index from the calling collection.

# Python program to access front and rear
# elements from Tuple

import operator

# Initializing and printing tuple
intTup = (4, 1, 7, 8, 5, 3)
print("The elements tuple are " + str(intTup))

# Accessing and printing front and rear 
# elements from the tuple 
frontEle = operator.itemgetter(0)(intTup)
rearEle = operator.itemgetter(-1)(intTup)

print("The front element of tuple is : " + str(frontEle))
print("The rear element of tuple is : " + str(rearEle))

Output:

The elements tuple are (4, 1, 7, 8, 5, 3)
The front element of tuple is : 4
The rear element of tuple is : 3

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.