Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am i getting the valueerror? [duplicate]

Tags:

python

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?

like image 507
Ori Avatar asked Nov 24 '25 21:11

Ori


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))
like image 96
kwkt Avatar answered Nov 26 '25 13:11

kwkt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!