Nothing remains after making a python map object a list

For the map / zip / filter object, do list twice and it will be empty.

list_twice


>>> xs = [1, 2, 3]
>>> mapped = map(lambda x:x+1, xs)
>>> list(mapped)
[2, 3, 4]
>>> list(mapped)
[]

I don't know what happened at first, it's a bug, or it's a destructive method! ?? I thought, but it seems to be a specification. http://stackoverflow.com/questions/19759247/listing-a-filter-object-twice-will-return-a-blank-list

So don't write it properly

Originally, I was addicted to making a dict as follows.

bad_code


xs = map(func, my_list)
ys = dict(zip([x.key for x in xs], xs))

It's a sad code to read back now ... It doesn't matter if it's included or not without using list (), it disappears when iterate, so in this case the second xs of the zip is empty. It was. So I decided to do my best to be careful when mapping.

By the way, the above code was solved as follows.

bad_code


xs = map(func, my_list)
ys = {x.key:x for x in xs}

Thanks to that, I learned that in Python3, comprehensions correspond to dict and set.

Recommended Posts

Nothing remains after making a python map object a list
Python: Get a list of methods for an object
Python list is not a list
What is a python map?
[python] Manage functions in a list
Touch a Python object from Elixir
python / Make a dict from a list.
Display a list of alphabets in Python 3
Create a JSON object mapper in Python
Map rent information on a map with python
[python] Get a list of instance variables
I made a Hex map with Python
Try drawing a map with python + cartopy 0.18.0
After studying Python3, I made a Slackbot
[Python] Get a list of folders only
[Python] list
[Python] List Comprehension Various ways to create a list
How to clear tuples in a list (Python)
[python] Create a list of various character types
Make a copy of the list in Python
Create a web map using Python and GDAL
Folium: Visualize data on a map with Python
View drug reviews using a list in Python
Create a datetime object from a string in Python (Python 3.3)
A simple to-do list created with Python + Django
A python amateur tries to summarize the list ②
Sort list elements in a specified order in Python
Insert an object inside a string in Python
About list processing (Python beginners after learning Ruby)
Visualize grib2 on a map with python (matplotlib)
[Python] Manipulating elements in a list (array) [Sort]
Tell me what a conformal map is, Python!
The result of making a map album of Italy honeymoon in Python and sharing it