When Python raises a TypeError because it cannot interpret a list object as an integer, inspect the value passed to the failing operation. The fix depends on what you meant that value to represent: an item, a count, or a single position.
For example, range needs an integer stop value. If a list arrives there instead, Python reports TypeError: ‘list’ object cannot be interpreted as an integer. A list can hold many values; Python has no reason to pick one of them as the stop value for you.
‘list’ object cannot be interpreted as an integer: find the input
Here is a small program that produces the error:
scores = [7, 9, 12]
for i in range(scores):
print(i)
The traceback points to the line containing range(scores), and its final line reads TypeError: ‘list’ object cannot be interpreted as an integer. Execution stops before the loop body runs.
Read the traceback from the bottom, then look at the indicated line. Identify the argument inside the operation: here it is scores. Add a temporary print immediately before that line:
print(repr(scores), type(scores))
# [7, 9, 12] <class 'list'>
This TypeError means that a list object cannot be used as the integer required by range. The list’s contents happen to be integers, but the outer object is still a list. Printing both value and type makes that distinction visible, even when the variable name suggests a count. In this TypeError, Python cannot treat the whole list as an integer for the stop value.
According to the Python documentation for ranges, range arguments must be integers or objects implementing the integer index protocol. A regular list does not implement that protocol. Python’s exception reference describes a TypeError as an operation receiving an inappropriate type. With that diagnosis, choose a repair based on the intended result.
Iterate over the items
If you want to process each score, loop over the list directly:
scores = [7, 9, 12]
for score in scores:
print(score)
# 7
# 9
# 12
Here, each pass assigns one item to score. There is no integer stop argument to supply, so the TypeError from interpreting a list as an integer disappears. The loop also continues to work if scores are added or removed.
If the position matters as well as the item, use enumerate. The built-in functions documentation explains that it supplies a count along with each item:
for position, score in enumerate(scores):
print(position, score)
# 0 7
# 1 9
# 2 12
This version is useful when labeling entries or reporting which one failed a check. It avoids a separate lookup and keeps each position paired with its value. By default, the count starts at zero; enumerate(scores, start=1) starts the displayed count at one instead.
Use a count when the operation needs one
Sometimes the desired result really is a sequence of positions. Use len(scores) as the stop value:
for i in range(len(scores)):
print(i, scores[i])
# 0 7
# 1 9
# 2 12
len(scores) returns 3, so range(3) produces positions 0, 1, and 2. The stop value is excluded. The documentation for len defines it as the number of items in a container. This count fixes the list-to-integer TypeError because the argument reaching range is now a scalar integer.
The distinction matters when the values look like counts. For [7, 9, 12], the length is 3, not 7, 9, or 12. If you need to repeat an action once per score, that is appropriate. If you need seven repetitions because the first score is 7, the length answers a different question.
An integer size parameter can produce the same kind of TypeError when given a list instead of an integer. For example, bytearray([3, 5]) creates a byte array containing those two byte values, while bytearray(len([3, 5])) creates two zero bytes. The bytearray documentation specifies these different meanings. Choose the form that matches the data you intend to create; a list is valid for some parameters even when a size elsewhere must be a number.
Select an element deliberately
Suppose a list stores possible repetition counts and the first one is the desired choice:
counts = [3, 5]
for i in range(counts[0]):
print(i)
# 0
# 1
# 2
Selecting counts[0] supplies 3. This repairs the TypeError caused by passing a list where an integer is expected only because the first element has the intended meaning and type. Check that the list is nonempty before accessing its first element; otherwise the access raises IndexError. Also check the selected value: an element can itself be a string or another list.
The same decision applies when a list reaches an index position. For example, letters[[1, 2]] tries to use a list as one index. If the goal is to retrieve two positions, iterate over them:
letters = ['a', 'b', 'c']
positions = [1, 2]
selected = [letters[i] for i in positions]
print(selected) # ['b', 'c']
If only one position is intended, choose that position explicitly, such as letters[positions[0]], after checking that it exists and is in range. The sequence documentation describes integer indexing and slicing. A list used as an index can trigger a TypeError about interpreting the object as an integer; changing the target list does not resolve the wrong index value.
Trace the value upstream
A variable can become a list several lines before the failure. A function might return a collection where its caller expected one count, or a loop might collect values that later reach a size argument. When the list object cannot be interpreted as the requested integer, follow the assignment and function return back to their source.
For example, if get_counts() returns [3, 5], decide whether the caller should process both counts or whether the function should return one chosen count. Avoid adding len simply to silence the exception: it returns 2 in this case, which is neither of the stored counts.
If TypeError: ‘list’ object cannot be interpreted as an integer persists, print the value and type immediately before the named parameter or index is used. Then trace where that value came from and choose the intended scalar, count, or iteration. A TypeError involving a list that cannot serve as an integer is a clue about the value at the point of use—not a request to discard the list.
