+ 2

Why doesn't this work?

I want to create a function that takes a list as an argument and returns the highest and lowest numbers in the list. But I keep getting an error TypeError: 'int' object is not iterable def min_max(lst): for x in list(lst): profit_lst = [min(x), max(x)] return profit_lst

19th Jul 2017, 6:29 PM
ΞⓀ⒟Ⓝ⒌Ξ
ΞⓀ⒟Ⓝ⒌Ξ - avatar
3 RĂ©ponses
+ 1
You don't need a for loop if you are using min or max on a list. def min_max(lst): profit_lst = [min(lst), max(lst)] return profit_lst val = [1,2,3,4,5,100] print(min_max(val))
19th Jul 2017, 6:34 PM
Jim Tully
Jim Tully - avatar
+ 1
I got it thank you guys.
21st Jul 2017, 5:14 PM
ΞⓀ⒟Ⓝ⒌Ξ
ΞⓀ⒟Ⓝ⒌Ξ - avatar
0
If lst is a list you don't have to iterate through list(lst) but through lst. Anyway, you don't need to iterate through it to get min and max because these functions take a list as argument. As lst is a list, you can write: def min_max(l): return (min(l), max(l)) ps : I return a tuple because I suppose it won't be modified
19th Jul 2017, 6:48 PM
clement
clement - avatar