How does `.pop()` work?

Hey, guys. That loop syntax is very hard to me. I really didn’t understood how to get items from that list with .pop, so I created a solution using .append to match the answer:

index = -1

while len(students_in_poetry) < 6:
students_in_poetry.append(all_students[index])
if len(students_in_poetry) < 6:
index += -1
else:
break
print(students_in_poetry)

2 Likes

Where does the .pop() statement go?

1 Like

The list.pop() method can be used in one of two ways.

  1. Last on, First off. This means it will always take the element at the end of the list.
  2. Selectively, which means at any valid index.
item = items.pop()

item will be the last value in the items list.

item = items.pop(index)

item will be the value at items[index].

We want the six students at the end of the all_students list.

student = all_students.pop()
52 Likes

Hi Roy,

I tried that:

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

index = 0
while len(students_in_poetry) < 6:
students_in_poetry = all_students.pop()
print(students_in_poetry)
index += 1

but I have this answer

Value for students_in_poetry did not match ['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora'] , (was Minerva ) ???

I tried many value to replace < 6 but it doesn’t work .
Something else, I don’t understand is why to use .append (in get stuck), we cannot use only .pop ???

Thx

3 Likes

Wow, I get it. I was close…

2 Likes

While not telling me your solution, what was the idea that changed your function? I have tried many different variations, including something similar to what you wrote but I cannot seem to get the correct function.

best,

J

There are two things to keep in mind about .pop().

  1. What we pop can be assigned to a variable, or used in an expression.
  2. It shortens the list by one element.

Since we are using while, then what will the condition be set at? What are we to do with the popped element?

8 Likes

Having the same issue. This explanation/exercise is very poorly worded.

38 Likes

The lesson text is brief, but it does explain perfectly how while works. By now we should be well familiar with conditional expressions.

while condition:
    # do this

When the condition is not met, the loop ceases.

While the length of the students_in_poetry list is less than 6 , use .pop() to take a student off the all_students list and add it to the students_in_poetry list.

students_in_poetry = []
while len(students_in_poetry) < 6:
    # do this

Since the length of students_in_poetry is 0, the loop body code will execute.

student = all_students.pop()
students_in_poetry.append(student)

Notice that the students_in_poetry list is growing as we append each student, which makes sense. Once that list reaches 6 in length, the loop ceases.
:
There is a caution that we should be aware of, something that was brought up earlier… infinite loops. Can we predict what would cause the above loop to go on forever?

39 Likes

Great discussion, I want to add how I viewed it. Building onto the hint explanation the full answer is

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

We want to learn how we arrived at this.

Firstly, the task is to fill up poetry class students as long as the number of students falls below 6.
Next, we want to remove the students from the pool of “total students” to avoid double counting [as we plan to add them to poetry class].
Lastly, after removing the students from the pool of “total students” we add them through .append to students_in_poetry.

11 Likes

One more way to go about it:

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

print(students_in_poetry)
24 Likes

I solved it fairly similar:

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

while len(students_in_poetry) < 6:
  students_in_poetry.append(all_students[-1])
  all_students.pop()
print(students_in_poetry)

So I append the last student on the list, then remove them until the poetry class reaches a limit of 6 students.
Hope that it can inspire.

11 Likes

Can anyone explain why the code says < 6 for poetry students? Capped at 6 is supposed to mean =< 6 (if I’m not wrong). Also the use of .pop is not explained prior to this exercise so had to guess

3 Likes

The loop will keep iterating until that condition fails, which means there will be exactly 6 members in the capped list.

Not sure that pop has not been covered yet. It should have come up in the unit on lists and or dictionaries, which both have a pop method.

list.pop(index)

pops from anywhere in the list;

list.pop()

pops the last item from the list.

4 Likes

Hi i have worked upon different solution which does not use pop() function.

Below is the code :

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

def add_student(index):
students_in_poetry.append(all_students[index])
return (students_in_poetry)

index=-1
while index>(-7):
add_student(index)
if index == (-6):
print(students_in_poetry)
index+= (-1)

print(students_in_poetry)

The above solution is not using any pop(). I tried to use python that i have learned so far.
Kindly help whether finding the solution in this way is correct or not. Or using pop() has any added advantage that maybe i am missing.

Moreover , i can add starting name also : for ex - starting from Alex.

Kindly give any suggestions to improve this code also.

2 Likes

One advantage that pop offers is that it removes the item from the list, and lets us assign it to another list. Your method does nor remove the names that are assigned to the new list.

>>> all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
>>> students_in_poetry = []
>>> n = len(all_students)
>>> while len(all_students) > n - 6:
	students_in_poetry.append(all_students[-1])
	all_students = all_students[:-1]

	
>>> all_students
['Alex', 'Briana', 'Cheri', 'Daniele']
>>> students_in_poetry
['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora']
>>> 

Above we use list slicing to shorten the list on each pass. If you have not studied the slice method, then we will need to revert to an indexed approach.

>>> all_students = ["Alex", "Briana", "Cheri", "Daniele", "Dora", "Minerva", "Alexa", "Obie", "Arius", "Loki"]
>>> students_in_poetry = []
>>> n = len(all_students)
>>> m = n - 1
>>> while len(all_students) > n - 6:
	students_in_poetry.append(all_students[-1])
	del(all_students[m])
	m -= 1

	
>>> all_students
['Alex', 'Briana', 'Cheri', 'Daniele']
>>> students_in_poetry
['Loki', 'Arius', 'Obie', 'Alexa', 'Minerva', 'Dora']
>>> 
3 Likes

Thanks sir for the feedback

For 1st code with slicing method i have found some correction because it was not working.

below in corrected code:

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

students_in_poetry =

n = len(all_students)
while len(students_in_poetry) < 6:
students_in_poetry.append(all_students[-1])
all_students = all_students[:-1]

print(all_students)
print(students_in_poetry)

2nd code is working fine.

Thanks again for the help

1 Like

Hello! I have done the exercise the correct way, but had this originally and wanted to know why the += .pop() situation worked this way: I’m getting an output that is taking each letter of each name and making it into its own item. Why does .pop() in this case remove individual letters and not the entire item?

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

while index < 6:
students_in_poetry += all_students.pop()
index += 1
print(students_in_poetry)
print (all_students)

result:
[‘L’, ‘o’, ‘k’, ‘i’, ‘A’, ‘r’, ‘i’, ‘u’, ‘s’, ‘O’, ‘b’, ‘i’, ‘e’, ‘A’, ‘l’, ‘e’, ‘x’, ‘a’, ‘M’, ‘i’, ‘n’, ‘e’, ‘r’, ‘v’, ‘a’, ‘D’, ‘o’, ‘r’, ‘a’]
[‘Alex’, ‘Briana’, ‘Cheri’, ‘Daniele’]

3 Likes

students_in_poetry is a list. The only thing that can be concatenated to a list is another list.

... += [all_students.pop()]
6 Likes

Because +=, when used with lists, works like lst.extend(), i.e., it treats the right operand (or the argument, in the case of extend()) as an iterable, and appends each of the elements of the iterable to the list.

So when the Python interpreter sees this:

students_in_poetry += all_students.pop()

It first executes all_students.pop(), which returns, the first time around, "Loki".

… and then executes students_in_poetry += "Loki", which, as noted by @mtf, is treated as

students_in_poetry += ['L', 'o', 'k', 'i']

4 Likes