-4

The first time I run this block of code from Notebook it works fine:

#Which letters and how many
letters = ["a","b","c"]
noOfLetters = len(letters)

#Looking for all permutations
resultA = []
from itertools import permutations
for i in range(noOfLetters):
    resultA.append(list(permutations(letters,i+1)))

If I run it again (without restarting the Kernel) I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-4050a4ce7a36> in <module>()
      7 from itertools import permutations
      8 for i in range(noOfLetters):
----> 9     resultA.append(list(permutations(letters,i+1)))

TypeError: 'list' object is not callable
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
GDP
  • 1
  • 3
  • 3
    At some point after this code, you do `list = `. This shadows the built-in `list` and causes the issue. Rename it to `lst` or something else. Try to never shadow built-ins – Adam Smith Sep 05 '15 at 04:02
  • Thanks Adam, that is indeed it. – GDP Sep 05 '15 at 04:06
  • @GretelDePaepe if you *must* use the same name as a built-in, one convention is to append an underscore to the name: e.g., in this case, `list_ = . . .` that won't overwrite the built-in `list`. – abcd Sep 05 '15 at 04:15

1 Answers1

2

Assuming "notebook" is Jupyter (previously ipython notebooks), you must be careful that jupyter keeps the state of all variables.

--> that means that the second run starts with variables already initialized at the value they had at the end of the first run.

One way to avoid that is to restart the kernel; another is to delete all variables; one more is to initialize all your variables each time you run.

from the docs:

To restart the kernel (i.e. the computational engine), click on the menu Kernel -> Restart. This can be useful to start over a computation from scratch (e.g. variables are deleted, open files are closed, etc...).

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80