In the below code, why do I get different outputs? After iterating over the first arr
, I can't make a list out of it (it's empty). However, if I do the same before iterating, as seen in the second part of the code, the set gets created, but when iterating after that, it seems like arr2
is empty.
Why does this happen and how to avoid/fix it?
arr = map(int, [1,2,3])
for item in arr:
print(item)
myset = set(arr)
print(myset)
# Output:
# 1
# 2
# 3
set()
########################
arr2 = map(int, [1,2,3])
newset = set(arr2)
print(newset)
for item in arr2:
print(item)
# Output:
# {1, 2, 3}