i have two value:starid1,starid2,and a list called starlist, i want to pick up two object from the starlist that contains id starid1 and starid2,and there is no duplicated id in the list.
this is how i write the code
star1,star2=None,None
for x in starlist:
if x.id == starid1:
star1 = x
elif x.id == starid2:
star2 = x
here's another way
star1 = [x for x in starlist if x.id==starid1][0]
star2 = [x for x in starlist if x.id==starid2][0]
or i can convert it to a dictionary and pick the two objects.but i thought the cost was too high, for i only want to assign two values.
i felt so dumb when i wrote those codes down.i think i just missed the proper way to do it in python. tell me how you know to do it better.