Search code examples
pythonpython-3.xiteratorpython-itertools

I can't find imap() in itertools in Python 3


I have a problem that I want to solve with itertools.imap(). I imported itertools and called itertools.imap(), but apparently itertools doesn't have attribute imap. What's going wrong?

>>> import itertools
>>> dir(itertools)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_grouper',     '_tee', '_tee_dataobject', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']
>>> itertools.imap()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
itertools.imap()
AttributeError: 'module' object has no attribute 'imap'

Solution

  • itertools.imap() is in Python 2, but not in Python 3.

    Actually, that function was moved to just the map function in Python 3 and if you want to use the old Python 2 map, you must use list(map()).