delete all keys except one in dictionary

后端 未结 4 1950
眼角桃花
眼角桃花 2021-02-18 13:22

I have a dictionary

lang = {\'ar\':\'arabic\', \'ur\':\'urdu\',\'en\':\'english\'}

What I want to do is to delete all the keys except one key.

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-18 13:30

    pop() it via for loop like this

    [s.pop(k) for k in list(s.keys()) if k != 'en']
    

提交回复
热议问题