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

前端 未结 7 684
自闭症患者
自闭症患者 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:29

    Just in case, if you want lookup search on the basis of the key of a dictionary.

    my_item = next((item for item in my_list if item.has_key(my_unique_key)), None)

    For 3.0+, has_key() has been deprecated. Instead use in:

    my_item = next((item for item in mylist if 'my_unique_key' in item), None)

    https://docs.python.org/3.0/whatsnew/3.0.html#builtins

提交回复
热议问题