Antonio D'Costa
Antonio D'Costa

Reputation: 41

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

I'm getting that error when I try to run my program in python 3 in Conda version 4.9.2

Traceback (most recent call last): File "mosaic.py", line 1, in <module> from itertools import pairwise ImportError: cannot import name 'pairwise' from 'itertools' (unknown location)

Upvotes: 4

Views: 8569

Answers (2)

hamflow
hamflow

Reputation: 321

adding to @foobar answer, to use these modules, you can either install more-itertools which is replica of itertools extra modules for python >3.10, or just write the itertools function yourself by copying it from itertools documentation! it is pretty easy. for example for pairwise:

def pairwise(iterable):
    # pairwise('ABCDEFG') → AB BC CD DE EF FG
    iterator = iter(iterable)
    a = next(iterator, None)
    for b in iterator:
        yield a, b
        a = b

arr1 = np.array([10,20,30,40])
str1 = "ABCDEF"
print(  list(pairwise(arr1))   )
print(  list(pairwise(str1))   )

which prints:

#[(10, 20), (20, 30), (30, 40)]
#[('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F')]

Upvotes: -1

foobar
foobar

Reputation: 619

This function is available in Python^3.10 (Python docs).

Upvotes: 7

Related Questions