Search code examples
pythonimporterrorpython-itertools

py-me throws Unpermitted modules Exception for importing itertools


Am trying to compress a list using py-me but I get an Unpermitted modules Exception.

Here is my code:

def compress(li):
    from itertools import zip_longest
    return([i for i,j in zip_longest(li,li[1:]) if i!=j])

x = [1,2,3,4,5,6,6,6,6,7,7,7,8,9]

print(compress(x))

Py-me gave the following as response.

OUTPUT : Code was not executed due to Unpermitted Modules Exception EXCEPTION : Unpermitted Modules Exception : fromitertoolszip_longest <-^--- modules are not allowed to be used in py-me. Check documentation for permitted modules !

Thanks in advance.


Solution

  • According to py-me documentation, it seems that you cannot use from keyword or . (dot operator) in the import section.

    Instead of from itertools import zip_longest, try using import itertools and while calling the function, use itertools.zip_longest.

    The modified code looks something like this :

    def compress(li):
        import itertools
        return([i for i,j in itertools.zip_longest(li,li[1:]) if i!=j])
    
    x = [1,2,3,4,5,6,6,6,6,7,7,7,8,9]
    
    print(compress(x))
    

    If you wanna check a python snippet will run successfully in py-me API, try executing the snippet in py-me web editor first and modify the code accordingly.