0

The following simple code returns an error, even with list():

map(max,[1,2,3,4])
Out[123]: <map at 0xdff50f0320>
list(map(max,[1,2,3,4]))
TypeError: 'int' object is not iterable

I used list to map object is to display the content; it seems not work this way. How to see the map object?

2
  • 3
    map(max, [1,2,3,4]) calls max(1), then max(2), then max(3), then max(4). None of those calls make any sense. What did you want it to call? Just max([1,2,3,4]) without map? Or something different? Commented Aug 22, 2018 at 0:02
  • The reason you don't get an error from just map(max, [1,2,3,4]) without the list(…) is that you're just creating the map iterator. It doesn't try to call map(1) (which raises the exception) until you try to use the iterator. Commented Aug 22, 2018 at 0:03

1 Answer 1

1

map applies the given function to each element of the iterable object that follows. What you've tried to do is to take the maximum of four integers, individually. Since max also requires an iterable object, this fails.

Usage example:

l = [
      [1, 2, 3, 4],
      [3.14, 2.7, 6.023, -5],
      ['Python', 'Java', 'R']
    ]

large = map(max, l)
for big in large:
    print (big)

Output:

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

1 Comment

great. Thanks for pointing out max() is done individually. Also appreciate the point from the other comment why there's no error in map(). By the way, the example didn't mean to have any meaningful usage. Just highlight the error.

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.