typeerror: 'itertools.permutations' object is not callable in python

This error occurs when we try to call a non-callable object. The itertools.permutations() function returns an iterator object containing all permutations of given iterable in a tuple form.

Here's an example code that will raise the TypeError: 'itertools.permutations' object is not callable error:

main.py
import itertools

perms = itertools.permutations(['a','b','c'])

# Try to call perms (which is not a callable object)
perms()
126 chars
7 lines

To avoid this error, we need to iterate over the permutations object using a loop or convert it into a list and then we can access individual permutations by their indices.

Here's the correct code:

main.py
import itertools

perms = itertools.permutations(['a','b','c'])

# loop through permutations
for perm in perms:
    print(perm)

# or convert it to a list
perms_lst = list(perms)
print(perms_lst[0])
199 chars
12 lines

This will print all permutations of ['a','b','c'] using a loop. Alternatively, we can convert the permutation object to a list and then access the permutations using their indices.

gistlibby LogSnag