Home > Enterprise >  List comprehension - TypeError: 'int' object is not iterable
List comprehension - TypeError: 'int' object is not iterable

Time:12-18

I am using list comprehension to generate a list of products of two values, and am seeing a TypeError. A simplified example is below.

def exp_n(n):
    k = 5
    s = 1
    a = [5*max(x s-K) for x in range(0,n)]
    print(a)
    
exp_n(10)

TypeError: 'int' object is not iterable

From looking at other posts, it seems like this usually has to do with the iterable defined in the forloop returing a non-iterable. But here, range(0,n) is certainly iterable. Any ideas what the issue is?

CodePudding user response:

Base on the code I assume this is what you are looking for:

def exp_n(n):
    k = 5
    s = 1
    a = 5*max([(x s-k) for x in range(0,n)])
    print(a)
    
exp_n(10)

CodePudding user response:

    a = [5*max([x s-K]) for x in range(0,n)]

?

  • Related