Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 19 discussion

Actual exam question from Python Institute's PCAP
Question #: 19
Topic #: 1
[All PCAP Questions]

What would you used instead of XXX if you want to check weather a certain 'key' exists in a dictionary called dict? (Choose two.)

  • A. 'key' in dict
  • B. dict ['key'] != None
  • C. dict.exists ('key')
  • D. 'key' in dict.keys ( )
Show Suggested Answer Hide Answer
Suggested Answer: BD 🗳️
Reference:
https://thispointer.com/python-how-to-check-if-a-key-exists-in-dictionary/

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
Oracleist
2 months, 2 weeks ago
A,D C is not working at all B is checking if the value of key is different than None
upvoted 2 times
...
Acid_Scorpion
7 months ago
Selected Answer: AD
A - is correct D - is correct B is can't be correct, as it checks value, not key
upvoted 2 times
...
kosa997
9 months ago
A is wrong - this takes the value, doesn't check if key exists
upvoted 1 times
...
Bubu3k
10 months, 3 weeks ago
B fails in this particular case: dict = {"A1": 1, "A2": 2, "A3": 3, "A4": None} key = "A4" print(dict[key]!=None) So it's AD
upvoted 3 times
...
mlsc01
1 year, 2 months ago
Selected Answer: AD
Only A and D are correct. Technically there can be a key which has None as its value, then option B will fail, because it checks for the presence of a value, not the key itself. ######## sample code ######## d = {'key1': 1, 'key2': None} k = 'key2' print(d) if k in d: print(f'\'{k}\' exists in dict') else: print(f'\'{k}\' does not exist in dict') if k in d.keys(): print(f'\'{k}\' exists in dict') else: print(f'\'{k}\' does not exist in dict') # wrong way because it check for the presence of value, not the key itself if d.get(k) is not None: print(f'\'{k}\' exists in dict <-- using wrong way') else: print(f'\'{k}\' does not exist in dict <-- using wrong way') # wrong way because it check for the presence of value, not the key itself if d[k] != None: print(f'\'{k}\' exists in dict <-- using wrong way') else: print(f'\'{k}\' does not exist in dict <-- using wrong way')
upvoted 2 times
...
Netspud
1 year, 2 months ago
Selected Answer: AD
3 of them work, I vote AD (B is a bit ugly!) dict = {'key': 'Farts'} try: if 'key' in dict: print("Key exists (A)") if dict['key']!= None: print("Key exists (B)") if 'key' in dict.keys(): print("Key exists (D)") if dict.exists ('key'): print("Key exists (C)") except: pass Key exists (A) Key exists (B) Key exists (D)
upvoted 1 times
...
rotimislaw
1 year, 5 months ago
Selected Answer: AD
A&D are most Python. B also returns True but it's a check if a key isn't None and no if a key exists so I'd cut that answer first. > dict = {'key' : 'value'} > print('key' in dict) True > print(dict['key'] != None) True > print(dict.exists('key')) Traceback (most recent call last): File "./prog.py", line 4, in <module> AttributeError: 'dict' object has no attribute 'exists' > print('key' in dict.keys()) True
upvoted 3 times
...
jaimebb
1 year, 5 months ago
Selected Answer: BD
The only one that not works it is C, all the others works correctly.
upvoted 2 times
...
Dav023
1 year, 6 months ago
Selected Answer: AB
A, B and D are rights!
upvoted 2 times
...
ciccio_benzina
1 year, 7 months ago
'a' works also (there are a lot of mistakes in this website)
upvoted 3 times
...
Jnanada
1 year, 8 months ago
A. 'key' in dict D. 'key' in dict.keys ( )
upvoted 2 times
...
PremJaguar
1 year, 9 months ago
the print statement looks like PYTHON 2!!!
upvoted 1 times
...
bebi
1 year, 10 months ago
A and D are the correct answers. B needs try/except.
upvoted 1 times
...
macxsz
1 year, 11 months ago
Selected Answer: AD
A. 'key' in dict D. 'key' in dict.keys ( )
upvoted 2 times
...
efemona
1 year, 11 months ago
A, B & D are correct, but B checks if a key value is Not None. The most pythonic answer is A and D which checks the dictionary keys
upvoted 4 times
...
Mokel
1 year, 12 months ago
AD is the correct answer
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...