Why doesn't this work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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