List comprehension vs. lambda + filter

前端 未结 14 2125
挽巷
挽巷 2020-11-22 02:01

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.

My code looked like this:



        
14条回答
  •  孤街浪徒
    2020-11-22 03:01

    My take

    def filter_list(list, key, value, limit=None):
        return [i for i in list if i[key] == value][:limit]
    

提交回复
热议问题