I recently wrote a code that had all attributes stored separately in different arrays like
order_id = []
order_name = []
order_products = []
order_total = []
And when importing new orders through an API request I would check if I already have that order by doing
if new_order_id in order_id:
# do new order stuff
Now I want to change the code so that order is a class that has id, name, products and total as attributes and all orders are stored in an array called orders. Is there an easy way to check if the new order id matches the id of any order objects in the orders array?
if new_order_id in [order.id for order in orders]would be a simple way. Better to use a dictionary mapping id -> object instead though.new_order_idusenew_order.idwherenew_orderis an instance of your new class.new_order_id. It looks more likeorder['order_number']