ADVERTISEMENT
ADVERTISEMENT

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 »



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.