Find the Middle Item in a Python List

To find the middle item in a Python List (for both odd and even number of items):

my_list = [1, 2, 3, 4, 5]

if not my_list:
raise ValueError("List is Empty")
else:
mid_item_index = len(my_list) // 2
if len(my_list) % 2 == 0:
mid_item_value = my_list[mid_item_index - 1: mid_item_index + 1]
else:
mid_item_value = my_list[mid_item_index]

print(mid_item_value)

The above code snippet first checks if “my_list” is empty. If it is, it raises a “ValueError” indicating that the list is empty. If the list is not empty, it calculates the index of the middle item in the list (“mid_item_index“) and then checks if the length of the list is even or odd. If it’s even, it takes the two elements at the middle position (“mid_item_index – 1” to “mid_item_index + 1“) and assigns it to “mid_item_value“. If the length is odd, it simply assigns the value at the middle index to “mid_item_value“.

The result:

3

Function to Find the Middle Item

Here is a function to find the middle item in a Python List:

def find_mid_item(my_list):
"""
Find the middle item in a list.
If the list contains an even number of items, return the two middle items.
If the list contains an odd number of items, return the single middle item.

:param my_list: The input list
:return: The middle item/s in a list
"
""
if not my_list:
raise ValueError("List is empty")
else:
mid_item_index = len(my_list) // 2
if len(my_list) % 2 == 0:
return my_list[mid_item_index - 1: mid_item_index + 1]
else:
return my_list[mid_item_index]


example_list = [1, 2, 3, 4, 5]

print(find_mid_item(example_list))

The result:

3

Test the function

To test the “find_mid_item” function across different scenarios using Pytest:

import pytest

# Assuming the find_mid_item function is stored under app.list_operations
from app.list_operations import find_mid_item


@pytest.mark.parametrize(
"input_list, expected_output",
[
([1, 2, 3, 4, 5], 3),
([1, 2, 3, 4, 5, 6], [3, 4]),
([55], 55),
([55, 66], [55, 66]),
(["a", "b", "c", "d", "e"], "c"),
(["a", "b", "c", "d", "e", "f"], ["c", "d"]),
([], None),
],
)
def test_find_mid_item(input_list, expected_output):
"""
Test the find_mid_item function across different lists.

:param input_list: The input list to be tested.
:param expected_output: The expected middle item/s in the input list tested.
"
""
if not input_list:
with pytest.raises(ValueError):
find_mid_item(input_list)
else:
assert find_mid_item(input_list) == expected_output

The result:

============================= test session starts =============================
collecting ... collected 7 items

test_list_operations.py::test_find_mid_item[input_list0-3] PASSED [ '14%']
test_list_operations.py::test_find_mid_item[input_list1-expected_output1] PASSED [ '28%']
test_list_operations.py::test_find_mid_item[input_list2-55] PASSED [ '42%']
test_list_operations.py::test_find_mid_item[input_list3-expected_output3] PASSED [ '57%']
test_list_operations.py::test_find_mid_item[input_list4-c] PASSED [ '71%']
test_list_operations.py::test_find_mid_item[input_list5-expected_output5] PASSED [ '85%']
test_list_operations.py::test_find_mid_item[input_list6-None] PASSED ['100%']

============================== 7 passed in '0.02s' ==============================

Additional Exercises

Leave a Comment