Python: How to check if keys exists and retrieve value from Dictionary in descending priority

前端 未结 4 2065
忘掉有多难
忘掉有多难 2021-01-03 19:07

I have a dictionary and I would like to get some values from it based on some keys. For example, I have a dictionary for users with their first name, last name, username, ad

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 19:50

    You can use myDict.has_key(keyname) as well to validate if the key exists.

    Edit based on the comments -

    This would work only on versions lower than 3.1. has_key has been removed from Python 3.1. You should use the in operator if you are using Python 3.1

提交回复
热议问题