0
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

2
  • I don't think map does what you think it does, for this example you don't need map you could just have v = mean(my_list), it would only be helpful if you had a list of lists you want to mean, like my_list=[[1,2,3,4,5],[1,1,4,43,2],[3,12,4,3,2]] Commented Mar 11, 2022 at 3:54
  • Please always format your code. Commented Mar 11, 2022 at 7:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.