Python | Rear elements from Tuple Strings

In this program, we are given a tuple consisting of string elements. We need to create a Python program to extract rear elements from tuple strings.
Submitted by Shivang Yadav, on November 07, 2021

Python tuples are commonly used in programs. And extracting elements and specific portions from them is quite difficult due to their immutability. Here, we will see a program to extract rear elements from tuple strings.

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)

Extracting rear elements from tuple String

In this program, we have a tuple with string elements. And we need to create a python program to extract all elements from the tuple of string elements.

Input:
("python", "learn", "includehelp")

Output:
["n", "n", "p"]

Method 1:

One simple way to solve the problem is by traversing the tuple and then for each string element extract the (n-1)th index value. This is done in a string line of code using list comprehension.

# Python Program to extract rear elements 
# from the tuple string 

# Creating and printing tuple string 
myTuple = ("python", "learn", "includehelp")
print("The elements of the tuple string are : " + str(myTuple))

# Extracting the last element from each 
# string element of the tuple
rearEleList = list(val[len(val) - 1] for val in myTuple)

# Printing the element list
print("The rear elements of each string from the tuple : " + str(rearEleList))

Output:

The elements of the tuple string are : ('python', 'learn', 'includehelp')
The rear elements of each string from the tuple : ['n', 'n', 'p']

Method 2:

Another method to perform the task is by directly using a loop to iterate over each element of the tuple. For each string of the tuple, extract the rear element i.e. element at (n-1)th index.

# Python program to extract rear elements 
# from the tuple string 

# Creating and printing tuple string 
myTuple = ("python", "learn", "includehelp")
print("The elements of the tuple string are : " + str(myTuple))

# Extracting the last element from the 
# each string element of the tuple
rearEleList = []
for strVal in myTuple :
    N = len(strVal) - 1 
    rearEleList.append(strVal[N])

# Printing the element list
print("The rear elements of each string from the tuple : " + str(rearEleList))

Output:

The elements of the tuple string are : ('python', 'learn', 'includehelp')
The rear elements of each string from the tuple : ['n', 'n', 'p']

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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