Python while loops

can i help with this question, i didnt post my code because literally none of it has worked and none of it has gotten me closer to understanding how to pop a list and add it to another list. ive read the question multiple times as well as the explanation but this specific problem just isnt clicking. just hoping i can get some fresh eyes on it so i can look it from a different perspective.

Hello there, even if your code doesn’t work, it can be helpful to post it to see your thought process and what concepts you’ve nailed down, as well as any potential syntax errors.

.pop(), without any arguments, removes the last item from the list it is used on. It also returns the item it just removed. You can find more information about the .pop() method here.

my_list = ["a", "b", "c", "d", "e"]

my_list.pop() # removes "e" from my_list

print(my_list.pop()) # removes "d" from my_list and prints "d" to the console

A while loop executes while the condition is True. The condition is checked at the beginning of each iteration.

my_list = ["a", "b", "c", "d", "e"]

while len(my_list) > 3:

print(my_list) # prints ["a", "b", "c"]

With this information, how could you solve the exercise?


so i coded this alot of the code is very buggy it pops out the last three but i dont know how to capture what it pops and append it. i looked up the error and im getting some back about using int() but i think thats overthinking for this problem what you see on where im going wrong?

i = students_in_poetry
while i < all_students:

This statement here is comparing i, which is assigned a reference to students_in_poetry to an integer.

Firstly, I believe you meant to use the len() function on i, which returns the length of a given list. This is because you can’t compare if a list is less than an integer. More on that function here.

Secondly, it isn’t necessary to use i at all. You can simply use the students_in_poetry variable throughout your code, including in your boolean condition. i is assigned a reference to students_in_poetry, not the actual value, meaning that it is essentially equal to students_in_poetry throughout your entire program. More on passing by reference vs. passing by value here.

After correcting the above, read the reply I posted earlier and make use of it to fix the code inside your loop.


ok sorry for still not getting it lol so far I tried 2 solutions they both give me the same error of unhashable type: “list”

and they both print out the same thing which is `[[‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’, ‘Dora’, ‘Minerva’, ‘Alexa’]]

not sure why its giving me the wrong answer this time i think its correct im appending the students that left after popping the last 3.


A few things to note:

Line 5: your while loop is checking if len(all_students) > 6. Note in the instructions that what you want to be checking is if students_in_poetry is less than 6.

Lines 6, 7 and 8: you are using all_students.pop() to take items from the list all_students, but you aren’t saving that item anywhere. This statements pop the items into oblivion. You need to define a variable to store your .pop(). See what I mean?

Line 11: When you do students_in_poetry.append(all_students) you are appending a list into another list. Notice how when the print in the next line prints students_in_poetry, it does so with double brackets [[ and ]], showing that indeed it’s a list inside a list. You need to fix this logic.

so what are you actually try to achieve?

  1. Define the comparison of the while loop to check if the length of students_in_poetry list is less than 6. Since in line 2 the list is initialized empty, you know this statement will be initially true and the while loop will execute.
  2. Using .pop(), take one student out of the all_students list, storing that popped element in a variable.
  3. Using .append(), add the student you popped in step 2 to the students_in_poetry list.
  4. No more statements in your while loop. Which will mean it will check its comparison at the top (check if the length of students_in_poetry is less than 6) and if it holds true, it will run the code inside the loop again (steps 2 and 3). Since you are appending a student to students_in_poetry every loop iteration (the list is growing in length by one), you know the comparison statement of the loop will eventually be false (your list will longer than 6) and the while loop will stop running.

thank you! I recently got it although I really feel like I cheated because I just basically appended it manually one by one and it passed. my issue is that the learn section doesn’t really cover the question I think they should rewrite it they dont even cover the pop method

Yes, I agree with your point on the .pop() method. It wasn’t explained very well in the course, though I do believe it was very briefly mentioned at some point.

Here’s the cheatsheet on the Lists unit and here’s the one on the Loops unit; they may be of help. :slight_smile:

1 Like