Python: casting map object to list makes map object empty?

Viewed 8158

I have a map object that I want to print as a list but continue using as a map object afterwards. Actually I want to print the length so I cast to list but the issue also happens if I just print the contents as follows:

print("1",my_map)
print("1",list(my_map))
print("2",my_map)
print("2",list(my_map))

and this gives me the following outputs.

1 <map object at 0x7fd2962a75f8>
1 [(1000.0, 1.0, 0.01, 0.01, 0.01, 0.01, 0.01)]
2 <map object at 0x7fd2962a75f8>
2 []

Why is this happening and how can I avoid it to continue using the map and its contents?

3 Answers

I faced the same issue since I am using Python 3.7 version. Using list(map(...)) worked. For lower python version using map(...) would work fine, but for higher versions, map returns an iterator pointing to a memory location. So print(...) statement will give the address rather than the items itself. To get the items try using list(map(...))

enter image description here

Related