[Tutor] inconsistent destruction

David Hutto dwightdhutto at gmail.com
Thu Aug 8 06:15:15 CEST 2013


if the variable is the range in the first one, then just don't append it,
and replace it with something else.

The second, you use cnt2, but it is a part of the the list comp, but not a
variable:

#this is in [ython 3, but you can import from future, or remove quotes from
print parameters
lst = [cnt2 for cnt2 in range(5)]
print(lst)

you want cnt2, but cnt2 is in the list comprehension, so it is a variable
within the list comp, but after it has been used there, then you need to
iterate through the list(lst), in order to find particular cnt's within the
lst list of cnt2 variables..



On Wed, Aug 7, 2013 at 11:54 PM, Jim Mooney <cybervigilante at gmail.com>wrote:

> This bugs me for some reason. The final variable is saved in a for
> loop but not in a list comprehension. It just seems to me they should
> both be destroyed to avoid confusion.
>
> lst = []
> for cnt in range(5):
>     lst.append(cnt)
> cnt
> 4
>
> lst = [cnt2 for cnt2 in range(5)]
> cnt2
> builtins.NameError: name 'cnt2' is not defined
>
> Is there a form of for loop that would destroy the loop variable? I
> could always do del cnt right after the for, but that seems
> artificial.
>
> --
> Jim
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130808/cbc58a69/attachment.html>


More information about the Tutor mailing list