0

I am new to python and was learning its builtins but function all() is not behaving as expected i don't know why.Here is my code

n=map(int,input().strip().split())
print(all([j>0 for j in n]))
print(list(n)) #this line returning empty list

Here are my inputs:

1 2 3 4 5 -9

And my output:

False

Does function all changes the original map object(values)? but something like this is not mentioned in given function definition on the docs link.

Thanks in advance

rahlf23
  • 8,869
  • 4
  • 24
  • 54
Devesh
  • 7
  • 1
  • 4
  • `all()` returns `True` if everything passed to it is `True`. You have -9 in your input so `j>0` for that is `False` so `all` returns `False`. What are you hoping to do to your input? – T Burgis Sep 26 '18 at 15:49

1 Answers1

2

Map returns a generator object which you exhausted in your all function. Therefore when you call list on n, since n is empty/exhausted, it returns an empty list.

To fix just make n a list in the first place.

n=list(map(int,input().strip().split()))
print(all([j>0 for j in n]))
print(n)
MooingRawr
  • 4,901
  • 3
  • 24
  • 31