Introduction to the “int object is not iterable” Error
The “int object is not iterable” error is a common issue that programmers encounter when working with Python. It typically occurs when you try to iterate over an integer using a loop or a built-in function that expects an iterable object, such as
list(). This error indicates that the operation you are performing is not supported on integer values. In this article, we will explore the common causes of the “int object is not iterable” error and provide you with solutions to resolve it.
Cause 1: Attempting to Iterate Over an Integer
Iterating Over an Integer
The primary cause of the “int object is not iterable” error is attempting to iterate over an integer directly. For example:
number = 123 for digit in number: print(digit)
In this example, the
for loop attempts to iterate over the
number variable, which is an integer. However, integers are not iterable objects, and you cannot loop over their individual digits.
Solution 1: Convert the Integer to an Iterable Object
Converting an Integer to an Iterable Object
To resolve the “int object is not iterable” error, you need to convert the integer to an iterable object before iterating over it. One common approach is to convert the integer to a string and iterate over its characters. Here’s an example:
number = 123 for digit in str(number): print(digit)
In this example, we convert the
number variable to a string using the
str() function. The resulting string is an iterable object that can be looped over to access its individual characters.
Cause 2: Incorrect Usage of Iterable Functions
Incorrect Usage of Iterable Functions
Another cause of the “int object is not iterable” error is incorrect usage of iterable functions, such as
list(), on an integer. These functions expect an iterable object as input, but when you pass an integer, it raises the error. For example:
number = 123 squared = map(lambda x: x ** 2, number)
n this example, we are attempting to square each digit of the
number using the
map() function. However, since
number is an integer, not an iterable object, the error is raised.
Solution 2: Wrap the Integer in an Iterable Container
Wrapping the Integer in an Iterable Container
To address this issue, you need to wrap the integer in an iterable container, such as a list or tuple, before using iterable functions. Here’s an example:
number = 123 squared = map(lambda x: x ** 2, [number])
In this example, we wrap the
number in a list
[number] before passing it to the
map() function. This ensures that the function receives an iterable object containing the integer.
The “int object is not iterable” error occurs when you attempt to iterate over an integer or misuse iterable functions on an integer. By understanding the causes and applying the appropriate solutions, you can overcome this error in your Python programs. Remember to convert integers to iterable objects or wrap them in iterable containers when necessary. With these techniques, you can avoid the “int object is not iterable” error and ensure smooth execution of your Python code.
My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.