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

Problem with for loop with list if list is empty
I've found a problem with the for loop block. Technically it's not a bug I guess, but it is annoying. If you have it like this: [scratchblocks] for ((i) :: control) = (1) to ([length v] of (list) :: list) { ... } @loopArrow :: control [/scratchblocks] if the list is empty, it will evaluate ... More on forum.snap.berkeley.edu
🌐 forum.snap.berkeley.edu
14
0
October 2, 2021
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
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
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
🌐
Stack Overflow
stackoverflow.com › questions › 66836241 › can-i-loop-an-empty-list
python - Can I loop an empty list? - Stack Overflow
And yes, you can loop over an empty list, but there will be 0 iterations. ... Save this answer. ... Show activity on this post. No, you can't iterate over an empty list because there are no items in it.
🌐
Snap! Forum
forum.snap.berkeley.edu › bug reports › snap! bugs
Problem with for loop with list if list is empty - Snap! Bugs - Snap! Forum
October 2, 2021 - If you have it like this: ... [/scratchblocks] if the list is empty, it will evaluate to this: [scratchblocks] for ((i) :: control) = (1) to (0) { ... } @loopArrow :: control [/scratchblocks] This makes the for loop go ...
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?

🌐
DataCamp
datacamp.com › tutorial › python-empty-list
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`
Find elsewhere
🌐
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.
🌐
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 - You will commonly see square brackets [] being used to create empty lists in Python because this syntax is more concise and faster.
🌐
Developer Diary
varunver.wordpress.com › 2017 › 06 › 29 › python-iterate-over-a-list-and-check-if-its-not-empty
Python – Iterate over a list and check if it’s not Empty
June 26, 2021 - When I used a list comprehension, it would fail if the list was None. But it would if the list was empty []. The old and easy way of doing this was: if tags: for t in tags: # Do stuff with t · The pythonic way of merging the if statement within the list comprehension is: for t in [t for t in (tags or [])]: # Do stuff with t ·
🌐
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
Don’t do this: Reserve try-except for handling actual exceptions (e.g., IndexError for out-of-bounds access), not empty lists. While len(my_list) == 0 works, if not my_list is more Pythonic and concise. Use len() only when you need the exact length. Prefer if my_list over len(my_list) > 0: Empty lists are falsy, so if my_list is cleaner and more idiomatic. Group loop logic with pre/post steps: Wrap the entire block (loop + messages/logging) in the if check to avoid misleading output.
🌐
Stack Overflow
stackoverflow.com › questions › 71655820 › how-to-not-print-empty-list-in-for-loop-python
How to not print "empty" list in for loop - Python - Stack Overflow
In this case, all you need to do is add an if: if lst: print(lst). Also: don't name your variable list, it shadows the builtin type list ... As input_name is a string, then this is looping over the characters in the string, meaning you attempt ...
🌐
Purple Engineer
purpletutor.com › home › understanding code › mastering python empty lists creation usage optimization techniques
Python empty list basics and methods explained 🔧📈
December 27, 2025 - Flexible Data Storage: An empty list is highly versatile, as you can add items of any data type (integers, strings, even other lists) later on, making it perfect for dynamic data collection. Prevents Initialization Errors: Starting with my_list = [] prevents runtime errors like `NameError` by ensuring the variable exists before it’s used in a loop or function call.
🌐
YouTube
youtube.com › watch
How To Check If A List Is Empty using Loops in Python #pybeginners - YouTube
Welcome to PyBeginners – your go-to place to learn Python programming the easy way! 🚀Today, we're checking for empty lists with simple code using some basic...
Published: August 30, 2025