delete all keys except one in dictionary

后端 未结 4 1935
眼角桃花
眼角桃花 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:46

    Iterate over keys() instead:

    for k in lang.keys():
        if k != 'en':
            del lang_name[k]
    

    If you're using Python 3 I believe you need to use list(lang.keys()) instead.

提交回复
热议问题