First and Last Element

Easy ⏱ 8 min 88% acceptance ★★★★☆ 4.4
Write first_and_last(items) that returns a tuple (first, last) containing the first and last elements of the list items. Use negative indexing for the last element. Assume items is never empty.

Examples

Example 1
Input
items = [10, 20, 30, 40]
Output
(10, 40)
Explanation

items[0] is 10, items[-1] is 40.

Example 2
Input
items = [7]
Output
(7, 7)
Explanation

A single-element list has the same first and last value.

Constraints

  • 1 <= len(items) <= 10^4

Topics

ListsIndexing

Companies

GoogleWipro

Hints

Hint 1

items[0] gets the first element.

Hint 2

items[-1] gets the last element without knowing the length.

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