Extracting Elements from Tuple

Write a Python program to get the 4th element and 4th element from the end of a tuple. Return these two elements as a new tuple.

Example 1:

Input: (5, 'hello', 8.3, 'world', 9, 'a', 'b', 6, 1) 
Output: ('world', 'a')

Example 2:

Input: (7, 'world', 3.14, 'd', 'new', 10, 'end', 9, 2) 
Output: ('d', 10)

You can use tuple indexing to access elements in a tuple. Remember, Python index starts from 0 and it also supports negative indexing.

def extract_elements(t):
    return (t[3], t[-4])

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!