Why does the code use less than 6 if it needs to stop at 6?

You are adding students to a Poetry class, the size of which is capped at 6.

while len(students_in_poetry) < 6:
student = all_students.pop()
students_in_poetry.append(student)

print(students_in_poetry)

There might be something I’m not seeing here, but if the class is capped at 6, why isn’t len(students_in_poetry)<=6? I tried it this way and it added a 7th student. I thought len(students_in_poetry)<6 would print 5 elements/students.

We go with less than since when there are 5 elements, the code is allowed to run once more. When we allow =6, then the code runs one more time after that, hence it reaches 7.

16 Likes

Understood. Thank you.

2 Likes

Because a list count from zero, not 1. In this example, 0 1 2 3 4 5 is six elements. A list with n length will have n elements with index from 0 to n-1.

Not exactly. The loop just repeats until there are already 6 elements in the list. For example, if there were 6 elements in the list (length of list = 6), it wouldn’t run the loop anymore since if it did it would become length = 7.

This is not a correct explanation.
We go less than six because this while loop is a first check-then perform. The last iteration will be when length to be equals to 5, at the end of the iteration length will be 6. At while loop will check again, the condition will not be met, and the loop ends.

9 Likes

When we are entering the While loop our len equals to zero and we add one student to the list. Then we go on the second round of the While loop and the len of poetry class is now one student – we add another student.
So we start with the zero len of the list and that’s why when our len is 5 we enter the while loop for the last time and at the end of this last time we are adding the 6th student.
We add a student after we check the len of the list.

1 Like

how about if they want us to add the students from the 0 index?
so far we adding from the last index but is there any other method we can use to get items from first index?

Yes, there is, the same method, only with an index specified.

list.pop(0)

Python is awesome
thanks

1 Like

Why is code allowed to run once more? That’s the key question. If we say < 6, that 6 refers to indexes. So 0 is a first element of the list. (0, 1, 2, 3, 4, 5) those are 6 elements. That’s why we say < 6.

From what I understand:
The ‘while’ loop checks the length of the list ‘students_in_poetry’. It doesn’t execute based on the index number; it executes upon checking if the ‘length < 6’ condition is True.
So on first iteration, the ‘students_in_poetry’ list is empty, i.e. length = 0
Upon the start of the second iteration, length = 1. And so on… Until the 7th iteration, when the length = 6. That’s when the length < 6 condition is not be met for the first time, since there are exactly 6 items in the list. That is when the loop stops, and doesn’t execute the .pop ( ) for the 8th time. Hence you are left with 6 items in the list.
I could be wrong :slight_smile: I’m still learning myself

1 Like

Great explanation but for one small detail… The 7th pop never happens.

@mtf please check this out too.
#another way

all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]

students_in_poetry =

i = 0

while i <= len(all_students):

tom = all_students.pop()

students_in_poetry.append(tom)

i += 1

print(students_in_poetry)

output: [‘Loki’, ‘Arius’, ‘Obie’, ‘Alexa’, ‘Minerva’, ‘Dora’]

It will also run 6 times as we are popping out elements from all_students.

1 Like

Any chance we could see the formatted source code?

@mtf
all_students = [“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, “Minerva”, “Alexa”, “Obie”, “Arius”, “Loki”]

students_in_poetry =

i = 0

while i <= len(all_students):

tom = all_students.pop()

students_in_poetry.append(tom)

i += 1

print(students_in_poetry)

output: [‘Loki’, ‘Arius’, ‘Obie’, ‘Alexa’, ‘Minerva’, ‘Dora’]

It will also run 6 times as we are popping out elements from all_students.
[/quote]

Why is the student = all_students.pop() line adding the removed student to the student variable and is it not just the list with the last student removed?

while len(students_in_poetry) < 6:
  student = all_students.pop()
  students_in_poetry.append(student)

When the .pop() method is used, it both removes the last element in a list (if no argument is provided) and also returns that element. For example, if I had lst = [0, 2, 4, 6] and used print(lst.pop()), 6 would be printed.

Here, three things are happening.

  1. The last element in all_students is removed.
  2. The removed element is assigned to the student variable.
  3. The student variable (and therefore the removed element) is appended to the end of students_in_poetry.
1 Like

Thanks for your explanation.

1 Like