Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I keep getting the error "min() arg is an empty sequence"?

Tags:

python

max

min

I have the following code:

test_file = open("test.txt","r")
numbers = test_file.readlines()
numbers = map(int,numbers)
print("Maximum number in list:", max(numbers))
print("Minimum number in list:", min(numbers))
test_file.close()

Can you help me please because I keep getting the error message: min() arg is an empty sequence. (I have the numbers 15, 30, 4, 9, 41, 76, 32 written into the file, and they are in different lines).

I get this output:

Maximum number in list: 76
Traceback (most recent call last):
  File "\\student-server\users$\16fvarela\Documents\YEAR 
9\CS\Notes\PYTHON\test.py", line 5, in <module>
    print("Minimum number in list:", min(numbers))
ValueError: min() arg is an empty sequence
like image 806
francisco Avatar asked Feb 05 '26 15:02

francisco


1 Answers

your numbers

numbers = map(int,numbers)

is an iterator (in python 3 that is; in python 2 you would have gotten a list and everything would have worked as expected); when you apply max(numbers) you exhaust the iterator; min has nothing to iterate over any more (i.e. min will complain about an empty sequence).

if your list of numbers is short enough you could fix that by

numbers = tuple(map(int,numbers))
like image 90
hiro protagonist Avatar answered Feb 07 '26 05:02

hiro protagonist