Say I have a class Box with two attributes, self.contents and self.number. I have instances of box in a list called Boxes. Is there anyway to access/modify a specific instance by its attribute rather than iterating through Boxes? For example, if I want a box with box.number = 40 (and the list is not sorted) what would be the best way to modify its contents.
Say I have a class Box with two attributes, self.contents and self.number. I have
Share
If you need to do it more frequently and you have unique
numbers, then create a dictionary:you can then access your boxes directly with numbers:
but if you want to change their number, you will have to modify the
numberedBoxdictionary too…Otherwise yes, you have to iterate over the list.