How does `.pop()` work?

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

44 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.

14 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

Interesting, thank you. I need to keep iterables in mind. I guess I thought .pop() would return a temporary list of just “Loki”, which += would concatenate it to the list of students_in_poetry.

2 Likes

Thanks for all the assistance on this question @mtf, much appreciated

1 Like

Thank you so much @tera3519027342 for asking the question and @mtf and @patrickd314 for the solution! I had this exact same problem and it was bugging me a lot, I was about to write a post asking about it when I noticed it had already been answered.

I found a very efficient way to tackle this exercise which I have not seen on here yet and thought I might share:

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

It’s beautiful how the .append and .pop functions work together in this excercise :heart_eyes:

5 Likes

Your example is also beautiful in that is employs methods that leverage the highest efficiency of the list type: adding to and removing things from the end of the list.

2 Likes

It really seems that pop method was not covered in the lists segment. Also there is no mention of it in the cheat sheet for lists. Looks like an oversight.

But, as always, google is your friend. A quick search gave me the explanation of the method, I then arrived at the same conclusion as @method1510630593 :slight_smile:

5 Likes

8 posts were split to a new topic: About documentation

totally! the only difference between your method and the .pop() way is that your leaves the students in the all_students list, while .pop would remove them.