CodexBloom - Programming Q&A Platform

Python Function Returning None Instead of Expected List After Modifying In-Place

๐Ÿ‘€ Views: 12 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-17
python functions list Python

I'm converting an old project and I'm sure I'm missing something obvious here, but I'm working with an scenario in my Python function where I'm trying to modify a list in-place, but the function ends up returning `None` instead of the modified list. I'm using Python 3.10 and the function is supposed to append items to a list based on some conditions. Hereโ€™s the relevant part of my code: ```python def add_items_to_list(original_list): for i in range(5): if i % 2 == 0: original_list.append(i) return original_list my_list = [] result = add_items_to_list(my_list) print(result) print(my_list) ``` I expected `result` to show `[0, 2, 4]` and `my_list` to have the same content. However, I got `[]` for both the `result` and `my_list`. After debugging, I noticed that my function modifies `original_list` correctly, but for some reason, the return value is not what I expect. Iโ€™ve also tried checking if the function is being called correctly, and I've confirmed that it is. Iโ€™m not sure why the return value is `None`. Iโ€™ve read that Python functions return `None` by default when there is no explicit return statement, but I do have a return statement. Could there be something I'm overlooking? Any insights would be appreciated! Thanks, I really appreciate it! Any pointers in the right direction?