Slice Fields Out of a Fixed-Width Record Tuple

Easy ⏱ 8 min 74% acceptance ★★★★☆ 4.4
Given a record tuple row = (id, name, department, salary, hire_year), implement contact_fields(row) returning a new tuple with only name and department (indexes 1 and 2), using tuple slicing. Then implement trailing_fields(row) returning the last two elements (salary, hire_year), also via slicing.

Examples

Example 1
Input
contact_fields((7, 'Neha', 'Engineering', 120000, 2021))
Output
('Neha', 'Engineering')
Explanation

row[1:3] slices out indexes 1 and 2.

Example 2
Input
trailing_fields((7, 'Neha', 'Engineering', 120000, 2021))
Output
(120000, 2021)
Explanation

row[-2:] grabs the final two elements.

Constraints

  • row always has exactly 5 elements in the stated order.

Topics

TuplesSlicing

Companies

WalmartFlipkart

Hints

Hint 1

row[1:3] slices indexes 1 up to (not including) 3.

Hint 2

row[-2:] takes the last two elements from the end.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.