Why am i getting the valueerror?

Viewed 79
l=['Python', 3, 2, 4, 5, 'version']
l=filter(lambda x:type(x)==int,l)
print(list(l))
print(max(l))

getting this error but i don't know why.. ValueError: max() arg is an empty sequence

if I am not printing list(l) it will work..

l=['Python', 3, 2, 4, 5, 'version']
l=filter(lambda x:type(x)==int,l)
print(max(l))

output: 5

after printing the list of the filter object it won't work and i don't know why can you help me? any fix?

1 Answers

filter returns an iterator. After calling list(l), the iterator is exhausted, and thus you can't draw any more values from it.

You can try this and see:

l = ['Python', 3, 2, 4, 5, 'version']
l = filter(lambda x: type(x) == int, l)
print(list(l))
print(list(l))

And the second print statement gives the empty list:

[3, 2, 4, 5]
[]

This would work:

l = ['Python', 3, 2, 4, 5, 'version']
l = filter(lambda x: type(x) == int, l)
l = list(l)
print(l)
print(max(l))
Related