Based on the other answers, I think the cleanest solutions are

#Handles None return from get_list
for item in get_list() or []: 
    pass #do something

or the comprehension equiv

result = [item*item for item in get_list() or []]
Answer from Tom Leys on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › add-values-into-an-empty-list-from-python-for-loop
Add Values into Empty List Using For Loop - Python - GeeksforGeeks
July 23, 2025 - The simplest way to add values to an empty list is by using append() method. This method adds a single item to the end of the list. Python · a = [] # Loop through a range of numbers and add them to the list for i in range(5): a.append(i) # ...
Discussions

python - For loop through the list unless empty? - Stack Overflow
I've been writing a lot of constructs like this the past couple of days: things = get_list() if things: for i in things: pass # do something with the list else: pass # do something ... More on stackoverflow.com
🌐 stackoverflow.com
for/empty loop condition in python - Stack Overflow
Your options are to peek (fetch ... after the loop. Only if the generator was empty and never produces the sentinel object would that condition still be true. 2019-02-21T12:31:41.217Z+00:00 ... @WloHu: for this question, where the object is clearly a list (my_list as a variable ... More on stackoverflow.com
🌐 stackoverflow.com
python - Would a for loop over an empty list actually spend time / resources? - Stack Overflow
This isn't really a question specific to any code, nor is it an issue I'm having, but for the sake of knowledge I'd like to know. Say, I have the following code: # list1 and list2 are determinied... More on stackoverflow.com
🌐 stackoverflow.com
November 4, 2015
language design - Why don't empty iterables in python raise Exceptions when you try to iterate over them - Software Engineering Stack Exchange
If you try to iterate over a generator with next and it's empty or has reached the end, a StopIteration exception is raised, but this is not the same when you use a for loop to iterate over a list or any iterable in Python. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 17, 2016
🌐
DataCamp
datacamp.com › home › tutorials › python › a comprehensive guide to python empty lists
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - In situations where the amount or type of data is unpredictable, empty lists emerge as flexible containers for dynamically accumulating information. Consider a program that reads user input until a specific condition is met. Here, we don’t know the number of user inputs we may get, and by using the .append() method, we are able to add elements to the list user_response dynamically. # Initialize an empty list to store user input user_responses = [] # Collect user input in a loop while True: response = input("Enter your response (or type 'exit' to finish): ") if response.lower() == 'exit': break user_responses.append(response) # User responses are now stored in `user_responses`
🌐
pythontutorials
pythontutorials.net › blog › for-loop-through-the-list-unless-empty
Python: How to Loop Through a List Unless It's Empty – Simplify Your Code — pythontutorials.net
Before diving into empty list handling, let’s recap how to loop through a non-empty list. Python offers two primary loop types for this: for loops (most common) and while loops (for index-based iteration).
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › python-empty-list-tutorial-how-to-create-an-empty-list-in-python
Python Empty List Tutorial – How to Create an Empty List in Python
June 18, 2020 - In the example below, we create an empty list and assign it to the variable num. Then, using a for loop, we add a sequence of elements (integers) to the list that was initially empty:
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create an empty list in python?
How to Create an Empty List in Python? - Be on the Right Side of Change
October 14, 2022 - my_list = [] creates the empty list and assigns it to the name my_list. for i in range(10): initializes the for loop to be repeated 10 times using loop variable i that takes on all values between 0, 1, …, 9.
🌐
GeeksforGeeks
geeksforgeeks.org › add-values-into-an-empty-list-from-python-for-loop
Add Values into Empty List Using For Loop - Python - GeeksforGeeks
December 6, 2024 - Python programming language supports the different types of loops, the loops c ... In Python, creating a list of lists using a for loop involves iterating over a range or an existing iterable and appending lists to the main list. This approach allows for the dynamic generation of nested lists based on a specific pattern or condition, providing flexibility in list construction.
Top answer
1 of 3
4

The thing that I think you're missing here is that the StopIteration is what actually makes the for loop stop. This is why you can write custom iterators that work with Python's for loop transparently. It doesn't do any checking to see if the iterator is empty, and the for loop does not keep track of of the iterator's state. This is an effect of the iterator protocol, and was an intentional choice in the language design. You can read more about the iterator protocol on Python's docs site if you want to.

It also makes more sense if you think about the number of items in e.g. your list corresponding directly to the number of times your loop is executed.

-----------------------------------|
| No. of items | No. times executed|
------------------------------------
|      5       |         5         |
|      4       |         4         |
|      3       |         3         |
|      2       |         2         |
|      1       |         1         |
|      0       |         0         |
------------------------------------

If the for loop special-cased an empty iterable, this invariant would be lost.

It would also complicate the protocol on writing custom iterators, because you would have to have an extra method to signal to the for loop that your iterable is empty before the iteration began.

Here's a (stupid) example of a custom iterator that always acts like it's empty:

class EmptyIterator():
    def __iter__(self): return self
    def __next__(self): raise StopIteration

for blah in EmptyIterator():
    print('this is never reached')
try:
    next(EmptyIterator())
except StopIteration:
    print('Oh hi, I was empty so I raised this Exception for you.')
2 of 3
10

This is odd to me. Why should an empty collection be treated any differently?

Forcing the programmer to check if the collection is empty before doing things to it would be a widespread, problematic sort of burden. Worse, an empty collection is in no way exceptional.

Personally, I would rather have the occasional bug where a no-op happened because I forgot to check for the rare case where I wanted different behavior on an empty collection rather than the occasional bug where an exception crashes my app because I forgot to say if empty, do nothing everywhere I wanted that common, expected behavior.

I mean, do you think printing empty strings should throw exceptions?

🌐
LabEx
labex.io › tutorials › python-how-to-handle-empty-list-iteration-430748
How to handle empty list iteration | LabEx
By understanding these basics, LabEx learners can effectively manage empty lists in their Python programming journey. ## Approach 1: Direct iteration with safety check empty_list = [] ## Safe iteration method for item in empty_list: print(item) ## No output, no error
🌐
Medium
medium.com › technology-nineleaps › how-to-add-elements-to-a-list-in-python-using-a-for-loop-39118a735df0
How to Add Elements to a List in Python Using a For Loop | by Nineleaps | Technology at Nineleaps | Medium
October 14, 2024 - Imagine you’re at the grocery ... into your cart. That’s what a for loop does—it iterates over data and appends each element to your list automatically. Let’s break this down with a clear, step-by-step process: First, create an empty list where you’ll store your ...