Removing elements from a Python list

Asked

Viewed 691 times

-1

Basically I want to remove the elements from a list by one criterion. For example: I have a room with N students, I will store the grade of these students in a list and compare these grades with the cutoff grade I type. If the student’s grade is higher or equal to the cut grade I leave it on the list, otherwise I remove it from the list

  • 1

    Show what you’ve done and what you’re having trouble with, specify your question and remember that Stackoverflow is not a place to ask people to do college/college "homework".

  • 1

    @Jeffersonquesado the title is almost the same, but the problems are different.

  • 1

    @Danielgomes true, I falter my. This one can go with list comprehension

1 answer

1

Only after the@AndersonCarlosWoss identified that there was another identical question, I saw that his answer is much wider than mine. See /a/190086/64969

Use list comprehension

Be it sala a student list. Be a student defined as having the attribute nota. Be it corte the variable of the cutting bike. Then we can get the list of approved students as follows:

aprovados = [ aluno for aluno in sala if aluno.nota >= corte ]

Thus, we get the list of approved students from a room without having to change the original value of the room list.

EDIT

My initial reading of the problem was that one had a list of students that needed to be filtered by note, but rereading the issue and, also, taking into account the comment of the AP, I realized that one has a banknote. My previous code remains valid, but we need to review some of the concepts used.

As we are with a list of notes, I have no object aluno attribute-ridden nota, only a number that already indicates the student’s grade. Thus:

Be it sala a list of student grades. Be corte the variable of the cutting bike. Then we can get the list of approved students as follows:

notas_aprovadas = [ nota for nota in sala if nota >= corte ]
  • I am declaring a list notes and from it I want to do it. I tried to implement following its logic and could not

  • Did you read how I was assuming you arrived at the student’s grade? I just tried to copy my code?

  • More importantly, did you read the @Andersoncarloswoss response? Both he posted as a comment and, shortly after he wrote the comment of possible duplication, I edited my reply and related to his reply?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.