# Unable to get the required output while looping

I’m trying to create a list of elements using the define and loop function from a list of elements excluding the max and min number in that list but I’m getting output as -1. My code is as under -

``````def notMaxNotMin(nums):
for number in nums:
if number != max(nums) and number != min(nums):
return nums
else:
return -1

a = [1,2,1,2,1,3,2,5,8]
b = [1,1,1,1,1,0,0,0,0,1,1,0]
numbers_1 = notMaxNotMin(a)
numbers_2 = notMaxNotMin(b)
print(numbers_1)
print(numbers_2)``````

@sunsamyan: Is `nums` a list you are iterating over? If so using `min()` and `max()` is not the right way to retrieve the minimum and maximum values. You may need a loop or list comprehension to compare the values in the list to derive the smallest and largest values using conditionals (`if`, `elif`, `else` etc.). Another way is to convert your list into a numpy array and then use the `np.max()` and `np.min()` properties to get the corresponding values. You may need to make the appropriate tweaks and research on how to iterate over numpy arrays.

Hope this helps!

1 Like

Actually, this is an assignment given at my college where I have write the code without using numpy. I have modified the code into following -

``````def notMaxNotMin(nums):
regular = []
for number in nums:
if number < max(nums) and number > min(nums):
regular.append(number)
return regular

def new_list(reg):
unique=[]
for digits in reg:
if digits not in unique:
unique.append(digits)
return unique
if len(unique) < 1:
return -1

a = [1,2,1,2,1,3,2,5,8]
b = [1,1,1,1,1,0,0,0,0,1,1,0]
number_1 = notMaxNotMin(a)
number_2 = notMaxNotMin(b)
number_3 = new_list(number_1)
number_4 = new_list(number_2)
print(number_3)
print(number_4)
``````

The code is working but for number_4 its reflecting an empty set instead of -1. Can you help?

1 Like

Here in `new_list()` function once it hit return it will not check further any code after `return` statement. So `if len(unique) < 1` condition will never get checked.

Hence you have to put final return at the end of the function

``````def new_list(reg):
unique=[]
for digits in reg:
if digits not in unique:
unique.append(digits)
if len(unique) < 1:
return -1
return unique
``````