+ 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
3 Respuestas
+ 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))
+ 1
I got it thank you guys.
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