pythonpython-3.xpython-itertoolspairwise

Pairwise not being imported with itertools


I'm on python 3.8.2 and I've tried the following code

from itertools import pairwise

However, it returns

ImportError: cannot import name 'pairwise' from 'itertools' (unknown location)

I tried other variants of the same thing like import itertools itertools.pairwise, but it still gave me errors.

I figured it could be a spelling mistake so I tried

from itertools import *

print(dir())

But it wasn't there

['__builtins__', '__file__', '__name__', '__warningregistry__', 'accumulate', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'filterfalse', 'groupby', 'islice', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee', 'zip_longest']

I even tried it on different devices and an online interpreter. screenshot of the online interpreter because this sounds crazy


Solution

  • This pairwise only comes with Python 3.10+. In previsous version, it is listed under Itertools Recipes section.

    That's why you cannot find it. Try to upgrade to latest version always.