Home > front end >  How do I remove items from a list based off of class data
How do I remove items from a list based off of class data

Time:11-19

class Student:

    def __init__(self, name, major, gpa, onProbation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.onProbation = onProbation

        Student1 = Student("Josh", "Business", 3.8, False)
        Student2 = Student("Maya", "Accountancy", 2.5, True)
        Student3 = Student("Dan", "Psychology", 1.2, True)
        Student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
        Student5 = Student("Michelle", "Medicine", 3.7, False)
        Student6 = Student("Joey", "Law", 4.0, False)

Students = ["Josh", "Maya", "Dan", "Keon", "Michelle", "Joey"]

I want to figure out how to remove all the students who are on probation from the list, so if I were to type print(Students) it would only give me the students that are not on probation (Josh, Keon, Michelle, and Joey)

CodePudding user response:

I believe I have found an answer to your question. I would also like to note that it is best not to create your student objects in the init method and storing all the students in a list is useful.. Here it is:

class Student:

    def __init__(self, name, major, gpa, onProbation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.onProbation = onProbation

students = [
    Student("Josh", "Business", 3.8, False),
    Student("Maya", "Accountancy", 2.5, True),
    Student("Dan", "Psychology", 1.2, True),
    Student("Keon", "Biomedical Engineering", 4.0, False),
    Student("Michelle", "Medicine", 3.7, False),
    Student("Joey", "Law", 4.0, False)
]

# I created a list for new students because it will mess up the for loop if you remove objects from a list while iterating
new_students = []
for student in students:
    if student.onProbation == False:
        new_students.append(student)

# print(students) will return an unreadable list with encoded numbers and such so I did this instead
for student in new_students:
    print(student.name,  student.onProbation)

I hope this was useful.

CodePudding user response:

class Student:

    def __init__(self, name, major, gpa, onProbation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.onProbation = onProbation

    @staticmethod
    def getStudentsNotOnProbation(students):
      res = []
      for student in students:
       if not student.onProbation:
         res.append(student)
      return res

Student1 = Student("Josh", "Business", 3.8, False)
Student2 = Student("Maya", "Accountancy", 2.5, True)
Student3 = Student("Dan", "Psychology", 1.2, True)
Student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
Student5 = Student("Michelle", "Medicine", 3.7, False)
Student6 = Student("Joey", "Law", 4.0, False)

Students = [Student1, Student2, Student3, Student4, Student5, Student6]


print(Student.getStudentsNotOnProbation(Students))

The reason I've used staticmethod so the logic should be encapsulated in Student class itself.

CodePudding user response:

this is kinda how i do it. When you make classes it remembers the attribute assignments even in lists. There called pylist in cpython


class Student:

    def __init__(self, name: str, major: str, gpa:float, onProbation: bool):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.onProbation = onProbation

# in your example you placed the class instances in the class, 
#   notice how mine are placed outside. The __init__ method initializes
#   the object, having it within the object is infinitly recursive i believe

student1 = Student("Josh", "Business", 3.8, False)
student2 = Student("Maya", "Accountancy", 2.5, True)
student3 = Student("Dan", "Psychology", 1.2, True)
student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
student5 = Student("Michelle", "Medicine", 3.7, False)
student6 = Student("Joey", "Law", 4.0, False)

students = [student1,student2,student3,student4,student5, student6]

mylist = students
for index, student in enumerate(students):
    if student.onProbation: # if the boolean isnt specified, defaults to true as is
        mylist.remove(students[index])

as John Gordon Pointed out, you could use list comprehension

class Student:

    def __init__(self, name: str, major: str, gpa:float, onProbation: bool):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.onProbation = onProbation



student1 = Student("Josh", "Business", 3.8, False)
student2 = Student("Maya", "Accountancy", 2.5, True)
student3 = Student("Dan", "Psychology", 1.2, True)
student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
student5 = Student("Michelle", "Medicine", 3.7, False)
student6 = Student("Joey", "Law", 4.0, False)

students = [student1,student2,student3,student4,student5, student6]

good_students = [x for x in students if not student.onProbation]

  • Related