Another question about pythonic syntax, yea!

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?



-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dardar via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dardar via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : leibylucw via Audiogames-reflector

Reply via email to