-2
n,m,k=map(int, input().split())
data=list(map(int, input().split()))

for i in data:
  print(i)
#consequence=2 3 4 5 6 
for i in data:
  print(max(i))

#consequence= TypeError: 'int' object is not iterable

If int is iterable, then why I can use int in for loop? Why I cannot use max() function in this case?

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
dy k
  • 27

3 Answers3

1

You can use max(x) like this:

data = [1,2,3,4,5]
max(m) # result 5

Int is not iterable but list is iterable. If you iterate you iterate trought list in for loop. How you use a for loop to find maximum doesnt make sense. Beacuse for every particular intiger you try to find a maximum. You can use this method as input of list.

  • Of the answers that got in here before it was closed as duplicate, I think this one comes closest to addressing the questioner's confusion. Please consider explicitly mentioning that they probably wanted `print(max(data))` instead of `for i in data: print(max(i))`. Also, you may be able to help them better if you look at [their first question on this website](https://stackoverflow.com/questions/71463346/why-the-code-shows-all-the-addition-process), which shows the same fundamental confusion about how loops in an imperative language work. Anyway, +1. – mtraceur Jul 04 '22 at 06:10
0

If you want to compare between two (or more) integers with max() you should provide them, at least two arguments. If you want to take the maximum of an iterable variable (like a string, for example max("123") >>> 3) you can provide one argument. in your case you provided one argument but it was int. think about it logicly, taking a maximum with one number doesn't really have a meaning (in math you take the number itself so maybe the confusion came from here).

Idan Rotbart
  • 93
  • 1
  • 5
-1

As the error says, 'int' object is not iterable. You are not using int in for loop. You are using data in for loop.

You can try

type(data)
list