Any other way to solve the same problem


#1

Hi everyone,
I was wondering if there is any other way to solve the same problem

Here’s my solution:


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

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

FAQ: Learn Python: Loops - While Loops
#2

Which problem?
Reversing it?
If you’re making some kind of copy then you shouldn’t need to modify the original. However, it does come with the benefit that there’s no need to keep track of a position in the list which is something that I generally like (hard to make mistakes when doing it that way)
If it’s reversed, then 6 seems like an oddly specific number, where does that come from?
If the copy is supposed to become some size then I’d probably phrase the condition as while that list is too small.


#3

Hi, just started de pro course and came across this problem. I was wondering if a shortened version was possible to answer the problem and came up with this:

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

while len(students_in_poetry) < 7:
students_in_poetry = all_students.pop()
print(students_in_poetry)

Or is this a shortcut that misses out on something (that i yet do not see?)


#4

l love seeing all the different ways we can generate the same solution. Below is what I put together.

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

print(students_in_poetry)

#5

Without using append, IF there already were one or more students enrolled in the students_in_poetry list they would get replaced by the name(s) at the end of the all_students list and we don’t want to kick out any students that are already enrolled in the course. You can test this by adding a student name to the students_in_poetry list before hitting Run.

Also, for reasons I do not understand, it seems your code returns 5 names instead of 6. If anyone can explain why, I would love to know the answer.


#6

They’re checking the length of strings instead of counting how many has been done/remain/equivalent
So if you for example made all long strings or all short strings you’d get all or nothing with that code


#7

You’re correct! Thanks :slight_smile:

Why there are only 5 i don’t know either. I put an 8 where there was a 7 ( while len(students_in_poetry) < 8:) and i got the whole list including an error : IndexError: pop from empty list …


#8

Your solution so neat and simple. Much appreciated for sharing it. :grin:


#9

My bad for not including the problem statement.:sweat_smile:
That 6 is an amount of given array/list, consisting of those names. It just came to my mind that I could’ve phrased while condition as comparing the lengths of two lists, i.e. while len(students_in_poetry) < len(all_students).