Suppose we have the following code.
x=[1,2,3,4,5,6,7,8,9,3,3,4,5,4,3] x=[item for item in x if item!=3]
Simple enough, right? We are removing an element from the list if it is equal to 3. However, this code will produce a traceback:
x=[1,2,3,4,5,6,7,8,9,3,3,4,5,4,3] x=[a for item in x if item!=3]
Running this will yield the following bit:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <listcomp> NameError: name 'a' is not defined
The same will happen if I run the code that looks like this:
x=[1,2,3,4,5,6,7,8,9,3,3,4,5,4,3] x=[a for item in x if a!=3]
The code will only work if I keep the same naming throughout the line, that is, [item for item in x if item!=3].
So, why? Why does the code throw a traceback when trying to do the 2 situations listed after the working solution? What is happening under the hood after I type in the first 2 lines of code? Are there any more similar situations in which the same constant rule would apply?
What is hard is making code that accepts different and sometimes unexpected types of input and still works.
This is what truly takes a large amount of effort on a developer's part.