Python Forum
how do i get y to be recognized in this comprehension?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i get y to be recognized in this comprehension?
#1
how do i get y to be recognized in this comprehension?

Output:
Python 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> [x for x in [y.lower(),y.upper()] for y in ['foo','bar']] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'y' is not defined >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
i think you mean this.
>>> [x for y in ['foo','bar'] for x in [y.lower(),y.upper()]]
['foo', 'FOO', 'bar', 'BAR']
The first for loop is the outer most loop. The way you have it written it is in the inner loop, but the upper method is in the outer loop causing a name error.
Recommended Tutorials:
Reply
#3
my thinking was that in a comprehension, the "for" followed what it controlled. but it guess not.

but wait, it does follow. now i'm even more confused.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I can recommend this talk by Trey Hunner starting at around 48:00 or watch it from the beginning.
Reply
#5
They follow suit with line after line in an expanded for loop except for what is controlled.
This is that list comp expanded. Every line follows the previous line except x which goes first.
for y in ['foo','bar']:
    for x in [y.lower(),y.upper()]:
        x
move second line to first line "after"
for y in ['foo','bar']:for x in [y.lower(),y.upper()]:
        x
remove colons
for y in ['foo','bar'] for x in [y.lower(),y.upper()]
    x
move x first
x for y in ['foo','bar'] for x in [y.lower(),y.upper()]
enclose the entire thing in brackets
[x for y in ['foo','bar'] for x in [y.lower(),y.upper()]]
more info including if conditions mixed in with it here
Recommended Tutorials:
Reply
#6
my thought process was that since the for followed the body, the outer for would follow what it applied to. instead, all the fors go together in regular order and the whole group follows what it all applies to.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 426 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  The term 'pip' is not recognized as the name of a cmdlet, function michaelnicol 1 626 Jul-16-2023, 11:12 PM
Last Post: deanhystad
  Index Function not recognized in Python 3 Peter_B_23 1 1,206 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,518 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  matplotlib isn't recognized after installation Pavel_47 5 2,819 Sep-18-2019, 07:01 PM
Last Post: snippsat
  why is my dictionary not recognized as such zatlas1 5 3,757 Jan-14-2019, 01:15 AM
Last Post: woooee
  pyserial-master installed bbut not recognized elwolv1 0 1,983 Jan-04-2019, 08:37 PM
Last Post: elwolv1
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 2,780 Oct-28-2018, 10:48 PM
Last Post: Gribouillis
  'videodigest' is not recognized as an internal or external command MM2018 2 2,773 Oct-12-2018, 02:43 PM
Last Post: MM2018
  Class attribute not recognized\working J125 1 5,376 Dec-19-2016, 01:05 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020