40

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'
mirekphd
  • 4,799
  • 3
  • 38
  • 59
  • 1
    It can be interesting also to take a look at [itertools.starmap](https://docs.python.org/3.6/library/itertools.html#itertools.starmap) in pyhton3. – med_abidi Dec 13 '16 at 06:21

6 Answers6

52

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()).

markzz
  • 1,185
  • 11
  • 23
  • 2
    thanks buddy, I was also trying to import accumulate but was not working. The problem was python2.x , Now switched to python3.X it starts working – Athar Aug 15 '16 at 17:47
17

If you want something that works in both Python 3 and Python 2, you can do something like:

try:
    from itertools import imap
except ImportError:
    # Python 3...
    imap=map
dawg
  • 98,345
  • 23
  • 131
  • 206
7

You are using Python 3, therefore there is no imap function in itertools module. It was removed, because global function map now returns iterators.

Konstantin
  • 24,271
  • 5
  • 48
  • 65
2

How about this?

imap = lambda *args, **kwargs: list(map(*args, **kwargs))

In fact!! :)

import itertools
itertools.imap = lambda *args, **kwargs: list(map(*args, **kwargs))
Inversus
  • 3,125
  • 4
  • 32
  • 37
  • This trick works for me. I want to get sum of all tuple in a list & this is a easy way to use it in python 3 – akshay_sushir Dec 22 '22 at 10:02
  • The `itertools.imap` function in Python 2 and the `map` function in Python 3 both return [generators](https://wiki.python.org/moin/Generators). While this answer will technically work, it calls `list` on the result set. This is fine if the results fit in memory but may not be desirable in other cases. – Patrick Mar 28 '23 at 17:49
1

I like the python-future idoms for universal Python 2/3 code, like this:

# Works in both Python 2 and 3:
from builtins import map

You then have to refactor your code to use map everywhere you were using imap before:

myiter = map(func, myoldlist)

# `myiter` now has the correct type and is interchangeable with `imap`
assert isinstance(myiter, iter)

You do need to install future for this to work on both 2 and 3:

pip install future
hobs
  • 18,473
  • 10
  • 83
  • 106
1

You can use the 2to3 script (https://docs.python.org/2/library/2to3.html) which is part of every Python installation to translate your program or whole projects from Python 2 to Python 3.

python <path_to_python_installation>\Tools\scripts\2to3.py -w <your_file>.py

(-w option writes modifications to file, a backup is stored)

user2757572
  • 463
  • 1
  • 4
  • 9