Question about while loop session

I have a question about the “while loop” session:
https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-loops/exercises/while?action=resume_content_item

When I tried to pop the last element from the list and add it to another list, the orden got changed and it showed the last six elements not the first six, any reason about it?

The result is showing as below:

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

all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
students_in_poetry = []
while len(students_in_poetry)<6:
  now = all_students.pop()
  print(now)
  students_in_poetry.append(now)
print(students_in_poetry)

Why did you expect the first 6 elements? Pop removes the last element from a list, which are then appended to a new list.

No specific reason, just curious. Is there any way to take the first six element + the new element from pop?

There is

due to the loop, pop removes the last element of the array 6 times. So i am bit confused what output you want to achieve, can you show me the final output you want to have?

Sorry, yes, like this

[“Alex”, “Briana”, “Cheri”, “Daniele”, “Dora”, ‘Loki’]

Then you could simple use a for loop to loop over the first 5 elements and then after the loop pop the last element? I think this a good challenge for you :slight_smile:

Wow, I think it works, thanks a lot for the tips!

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

students_in_poetry = []

while len(students_in_poetry) < 6:

  students_in_poetry = all_students[:5]
  now = all_students.pop()
  students_in_poetry.append(now)

print(students_in_poetry)