class Barcode:
def __init__(self, code, code_id = None):
self.code = code
self.id = code_id
I have a list of barcodes. How can I get a barcode with a specific id?
class Barcode:
def __init__(self, code, code_id = None):
self.code = code
self.id = code_id
I have a list of barcodes. How can I get a barcode with a specific id?
If your BarcodeList
isn't sorted, I recommend the brutal approach:
def find_item(id):
for item in BarcodeList:
if item.id == id:
return item
If it is sorted, see What is the best way to get the first item from an iterable matching a condition?
PS: Why not use dict
? It's born for doing this sort of job.