easy +5 pts

Middle Element of an Odd-Length List

Return the exact center item of a list that always has an odd number of elements.

Write a function `middle_element(values)` that takes a list `values` (which will always have an odd number of elements, i.e., length is odd) and returns the element exactly in the middle of the list. For example, for `[10, 20, 30, 40, 50]`, the middle element is at index 2, which is 30. **Function signature:** ```python def middle_element(values): pass ``` - The input list is non-empty and has an odd number of elements. - You can assume the list contains integers. - Do not modify the input list. Return the middle element as is.

Constraints

- 1 <= len(values) <= 10^6 - The length of `values` is always odd. - Elements are integers within Python's standard range.

Example

```python
>>> middle_element([1, 2, 3])
2
>>> middle_element([5, 6, 7, 8, 9])
7
>>> middle_element([42])
42
```
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The middle index is the length divided by 2 (using integer division).
For an odd-length list, the middle index is `len(values) // 2`.
Use list indexing to return the element at that index.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.