python "all" function making list empty
Question:
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
Answers:
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)
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
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)