I'd like to make sure that the Python Dictionary has a specific key

Asked 2 years ago, Updated 2 years ago, 102 views

I'd like to investigate if there is a specific key like the title. I made my own code. I'd appreciate it if you could teach me a better way.

if 'key1' in dict.keys():
    print "There is"
else:
    print "None"

python dictionary

2022-09-22 22:36

1 Answers

You don't have to bring a key from the dictionary. if 'key1' in dict: You can use this much. Or dict.has_key('key1') You can also check 'key1'.

You used in to check the key in dict so you wrote it well.


2022-09-22 22:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.