hasan
hasan

Reputation: 3

I have the error when i use list with function using MAP function

def mean(x):
   return sum(x) /  len(x)

my_list=[1,2,3,4,5]

v=list(map(mean,my_list))

when i run the code i face the following error:

return sum(x) / len(x)

TypeError: 'int' object is not iterable

Upvotes: 0

Views: 822

Answers (1)

Kenneth Mano
Kenneth Mano

Reputation: 36

To further explain some of what 35308 has mentioned in his comment, map takes each element from the list one-by-one and applies the mean function to it.

So, in your case, it will first assign x = 1, and then try to run mean on x.

When it does this, it will have sum(1)/len(1). Both sum and len require the argument being passed to it to be iterable, and an int is not an iterable.

If you just want the mean, just call mean on the list my_list, as the list is already iterable.

Upvotes: 1

Related Questions