Python Classes: How to update the class so that users can upload lists?

Hi! Here is my code for the Medical Insurance Project, where I incorporated the challenges except the last: how can I update the class so that users can upload lists of patient data rather than individual numbers?

At first this is what I did, but clearly this was wrong, since as I later realized I gave more positional arguments than what init accepts. Any suggestions?

class Patient:
  def __init__(self, list_of_patient_info): = list_of_patient_info[0]
    self.age = list_of_patient_info[1] = list_of_patient_info[2]
    self.bmi = list_of_patient_info[3]
    self.num_of_children = list_of_patient_info[4]
    self.smoker = list_of_patient_info[5]

You can add plenty of positional arguments to __init__ though like most callable options you should try to limit it to only a few terms just so it’s readable (self and a single list is absolutely fine). What’s your issue with the current set-up?

You could perhaps reduce the typing repetition afterwards (and make it a touch more flexible) if you assigned those attributes in a loop or something. A dict might be nicer than a list for this problem if it came up again (and wasn’t part of the instructions).

1 Like

I was on the same track as you, but I was trying to iterate through the list of patient info for several patients at one time rather than one at a time. I still want learn how to iterate through a nested list in the class, so I will keep working on it.

It looks like you forgot to loop through the items in your list first.
This is how I got it to work for one patient at a time:

class Patient:
  def __init__(self, patient):
    self.patient = patient
    for x in self.patient: = self.patient[0]
        self.age = self.patient[1] = self.patient[2]
        self.bmi = self.patient[3]
        self.num_children = self.patient[4]
        self.smoker = self.patient[5]

So if you have patient “John Doe”, put his info in a list:

patient1 = ["John Doe", 25, 1, 22.2, 0, 0]
John = Patient(patient1)

John Doe

1 Like

Thanks so much for the help everyone!