Search code examples
pythonlistintegertypeerror

What does TypeError: 'int' object is not subscriptable mean?


Question: Find if there is a palindromic integer in the list.
Solution: I iterated over the integer variable which showed a Typographic error. After typecasting each integer of the list with a string I was able to iterate over the list.

Output gave Type-error: 'int' object is not sub-scriptable

def function(n, L):
    # checking if number i == reverse of this number which is raising error
    print(any(i==i[::-1] for i in L))

if __name__ == '__main__':
    n = 5
    L = [2, 3, 5, 101, 42]
    function(n, L)

Solution

  • 'int' object is not subscriptable means you try to use [] on an int variable

    In you code, L is a list of integers, and you access each element in the for loop, then you try to take the int variable (represented as i) and access it with i[::-1], which is a Typeerror since i is an int not a list.