ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.