Python: get a dict from a list based on something inside the dict

前端 未结 7 683
自闭症患者
自闭症患者 2020-11-30 23:56

I need to be able to find an item in a list (an item in this case being a dict) based on some value inside that dict. The structure o

7条回答
  •  长情又很酷
    2020-12-01 00:24

    I used this, since my colleagues are probably more able to understand what's going on when I do this compared to some other solutions provided here:

    [item for item in item_list if item['id'] == my_unique_id][0]
    

    And since it's used in a test, I think the extra memory usage isn't too big of a deal (but please correct me if I am wrong). There's only 8 items in the list in my case.

提交回复
热议问题