How do I filter out non-string keys in a dictionary in Python?

前端 未结 5 1385
星月不相逢
星月不相逢 2021-01-24 07:13

I\'m new to Python and trying to figure out how to filter out all the non-string keys in a dictionary. I appreciate any help you can provide.

5条回答
  •  悲&欢浪女
    2021-01-24 07:47

    new = {k, v for k, v in old.items() if isinstance(k, str)} # repair items if key is string
    

提交回复
热议问题