Find the index of a dict within a list, by matching the dict's value

后端 未结 10 563
死守一世寂寞
死守一世寂寞 2020-11-28 18:56

I have a list of dicts:

list = [{\'id\':\'1234\',\'name\':\'Jason\'},
        {\'id\':\'2345\',\'name\':\'Tom\'},
        {\'id\':\'3456\',\'name\':\'Art\'}]         


        
10条回答
  •  自闭症患者
    2020-11-28 19:23

    I needed a more general solution to account for the possibility of multiple dictionaries in the list having the key value, and a straightforward implementation using list comprehension:

    dict_indices = [i for i, d in enumerate(dict_list) if d[dict_key] == key_value] 
    

提交回复
热议问题