filter items in a python dictionary where keys contain a specific string

后端 未结 5 1839
忘掉有多难
忘掉有多难 2020-12-02 11:44

I\'m a C coder developing something in python. I know how to do the following in C (and hence in C-like logic applied to python), but I\'m wondering what the \'Python\' way

5条回答
  •  误落风尘
    2020-12-02 12:13

    You can use the built-in filter function to filter dictionaries, lists, etc. based on specific conditions.

    filtered_dict = dict(filter(lambda item: filter_str in item[0], d.items()))
    

    The advantage is that you can use it for different data structures.

提交回复
热议问题