What Is an Iterable in Python?
An iterable is an object that Python can go through one value at a time.
You will see iterables all the time in Python. They are used in for loops, in built-in functions like sum() and sorted(), and in many common tasks such as reading files or unpacking values.
A big reason beginners should learn this word is that many errors and examples make more sense once you know what an iterable is. It also helps you understand the difference between an iterable and an iterator.
Quick example
items = [10, 20, 30]
for item in items:
print(item)
Output:
10
20
30
A list is iterable because Python can go through its values one by one.
Simple definition
An iterable is:
- An object Python can loop through
- Something that provides values one at a time
- Often used with
forloops
Common iterable objects include:
- Strings
- Lists
- Tuples
- Sets
- Dictionaries
rangeobjects
For example:
word = "cat"
for letter in word:
print(letter)
Output:
c
a
t
Here, the string "cat" is iterable because Python can loop through each character.
Why beginners need to know this
Many parts of Python expect an iterable.
For example, these built-in functions work with iterables:
sum()list()tuple()set()sorted()any()all()
Example:
numbers = [4, 5, 6]
print(sum(numbers))
print(list("abc"))
Output:
15
['a', 'b', 'c']
Understanding iterables also helps with error messages such as:
TypeError: 'int' object is not iterableTypeError: 'bool' object is not iterableTypeError: 'NoneType' object is not iterable
If you are still getting comfortable with loops, see Python for loops explained.
Common examples of iterable objects
Here are some common iterable types in Python.
List
values = [1, 2, 3]
for value in values:
print(value)
Tuple
values = (1, 2, 3)
for value in values:
print(value)
String
text = "abc"
for char in text:
print(char)
Set
values = {1, 2, 3}
for value in values:
print(value)
Dictionary
A dictionary is iterable, but it loops over keys by default.
student = {"name": "Ana", "age": 12}
for key in student:
print(key)
Output:
name
age
If you want the values or both keys and values, you would use .values() or .items().
Range object
The range() function creates an iterable object often used in loops.
for number in range(5):
print(number)
Output:
0
1
2
3
4
File object
A file object can be read line by line, which means it is iterable.
with open("example.txt") as file:
for line in file:
print(line.strip())
What is not iterable
Some values are not iterable.
Examples:
- A single number like
5 - A float like
3.14 - A boolean like
True None
This code causes an error:
value = 5
for item in value:
print(item)
Python raises a TypeError because an integer is not iterable.
If you need help with that specific error, see how to fix TypeError: 'int' object is not iterable.
Iterable vs iterator
These two words are related, but they are not the same.
- An iterable is something you can loop over
- An iterator is the object that gives the next value each time
You can turn an iterable into an iterator with iter():
numbers = [10, 20, 30]
iterator = iter(numbers)
print(next(iterator))
print(next(iterator))
print(next(iterator))
Output:
10
20
30
In this example:
numbersis an iterableiteratoris an iterator
You do not need to memorize the full iterator protocol yet. For a full beginner explanation, see what is an iterator in Python or iterators and iterable objects explained.
How Python uses iterables
Python uses iterables in many common situations.
In for loops
A for loop asks for items one by one:
colors = ["red", "green", "blue"]
for color in colors:
print(color)
If you want more loop examples, see how to loop through a list in Python.
In membership checks
The in operator often works with iterables:
print("a" in "cat")
print(2 in [1, 2, 3])
Output:
True
True
In unpacking
Python can unpack values from an iterable:
point = (4, 7)
x, y = point
print(x)
print(y)
Output:
4
7
In built-in functions
Many built-in functions read values from an iterable:
numbers = [3, 1, 2]
print(sorted(numbers))
print(any([False, False, True]))
print(all([True, True, True]))
Output:
[1, 2, 3]
True
True
How to check if something behaves like an iterable
For beginners, the easiest way is to test it in a practical way.
Try a for loop
value = "hello"
for item in value:
print(item)
If Python can loop through it, it behaves like an iterable.
Try iter()
value = [1, 2, 3]
iterator = iter(value)
print(iterator)
If iter(value) works, the object is iterable.
If it fails, Python raises a TypeError.
Example:
value = 3.14
iterator = iter(value)
This raises an error because a float is not iterable.
Common mistakes
Beginners often run into these problems:
- Thinking iterable means list only
- Confusing an iterable with an iterator
- Trying to loop over an
int,float,bool, orNone - Forgetting that dictionaries loop over keys by default
These quick checks can help when debugging:
type(value)
iter(value)
list(value)
for item in value:
print(item)
Be careful with list(value) and for item in value if you are not sure the value is iterable. They can raise a TypeError.
Related error pages:
FAQ
Is every iterable a list?
No. Lists are iterable, but strings, tuples, sets, dictionaries, range objects, and file objects are iterable too.
Is a dictionary iterable?
Yes. A dictionary is iterable, and looping over it gives its keys unless you use .values() or .items().
Is a string iterable?
Yes. Python can loop through a string one character at a time.
What happens if I try to loop over a number?
Python raises a TypeError because numbers are not iterable.
What is the difference between iterable and iterator?
An iterable is something you can start looping over. An iterator is the object that returns each next value during that loop.