def print_names(names):
for name in names:
print(names)
names = None
print_names(names)
Error trace:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-481a76948b73> in <cell line: 6>()
4
5 names = None
----> 6 print_names(names)
<ipython-input-26-481a76948b73> in print_names(names)
1 def print_names(names):
----> 2 for name in names:
3 print(names)
4
5 names = None
TypeError: 'NoneType' object is not iterable

Reason for the TypeError
As you can see, the names variable is declared as type None, you cannot iterate over None types in Python.
Fix:
It is better to first check if the type is not None before iterating.
def print_names(names):
if(names is None):
print("Names list is empty...")
else:
for name in names:
print(names)
names = None
print_names(names)
Output: Names list is empty...
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Java 8 foreach loop code examples - Java
- Android Studio NoClassDefFoundError: java.awt.Toolkit - Android-Studio
- Time Testing with Java JUnit assertTimeout method - Java
- [macOS] NetBeans IDE cannot be installed. Java (JRE) found on your computer but JDK XX or newer is required. - MacOS
- [fix] Spring Boot Data JPA - No identifier specified for entity - Java
- Fix: zipfile.BadZipFile: File is not a zip file Python - Python
- Flash Player will no longer be supported after December 2020. Turn off [Google Chrome] - Chrome
- Deep Dive: Java Object Class from java.lang Package - Java