francisco
francisco

Reputation: 37

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

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

Upvotes: 3

Views: 1051

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46849

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))

Upvotes: 6

SM Abu Taher Asif
SM Abu Taher Asif

Reputation: 2331

You can convert map to list:

numbers = list(map(int,numbers))

Upvotes: 0

Related Questions