Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 5 Online
OpenStudy (anonymous):

(Lecture 5, 20:00) Why the heck does this: flat = Techs + Ivys L1[0] = 3 print 'flat =', flat artSchools = ['RISD', 'Harvard'] print 'L2 =', L2 for u2 in artSchools: if u2 in flat: L1 = [2] flat.remove(u2) remove Harvard from flat? (Lecture 5 20:00) I do not understand the relationship between u2, L1,and [2].

OpenStudy (anonymous):

What exactly is [2]? Also what are the values of L1 and L2?

OpenStudy (anonymous):

Apparently they are the result of me copy/pasting part of the table on the side of the pdf when I was following along with IDLE. My question just got a simpler, but in the following program; Techs = ['MIT', 'Cal Tech'] Ivys = ['Harvard', 'Yale', 'Brown'] Univs = [] Univs.append(Techs) print 'Univs =', Univs Univs.append(Ivys) print 'Univs =', Univs for e in Univs: print 'e =', e flat = Techs + Ivys print 'flat =', flat artSchools = ['RISD', 'Harvard'] for u2 in artSchools: if u2 in flat: flat.remove(u2) I understand the basic list in artSchools and how flat is concatenated, but I do not get the significance of u2. Does it have anything to do with 'Harvard' being in the 2 position in flat? If so, how does the conditional statement checking for u2 in artSchools work out?

OpenStudy (rsmith6559):

u2 is just a variable. The for in statement iterates (goes through) the artSchools list, assigning each of it's values to u2 in turn. The if statement is if u2 is in the flat list, remove it.

OpenStudy (anonymous):

I see. I misunderstood the function of the "for in" loop. Am I to understand it in no way acts as a conditional statement? Anyway, thank you for your help, that really clarified this lesson for me.

OpenStudy (rsmith6559):

Am I to understand it in no way acts as a conditional statement? I wish you hadn't phrased it like that. The answer is that the for in and if statements aren't related and are separate steps. The pedantic answer is: iterating through an indexed data structure like a list has to know if the next iteration is still in the range of the list. Think of this conditional as internal to the for in statement.

OpenStudy (anonymous):

Sorry if I was imprecise, I meant the "for in" expression is not conditional on anything. I am aware that it did create the condition which triggered the "if in" statement.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!