PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 19


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

Show Answer
Correct Answer: AD

To check if a certain key exists in a dictionary, you can use the 'in' keyword, which is most Pythonic and directly checks for the key's presence. The expression 'key in dict' evaluates to True if the key is present in the dictionary. Another valid method is 'key in dict.keys()', which checks if the key is present within the dictionary’s keys. Therefore, the correct expressions to replace XXX are 'key in dict' and 'key in dict.keys()' as they efficiently and accurately verify the existence of a key in a dictionary.

Discussion

16 comments
Sign in to comment
efemonaOptions: AD
May 2, 2022

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

ciccio_benzinaOptions: AD
Sep 4, 2022

'a' works also (there are a lot of mistakes in this website)

rotimislawOptions: AD
Nov 14, 2022

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

Bubu3kOptions: AD
Jun 9, 2023

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

macxszOptions: AD
May 3, 2022

A. 'key' in dict D. 'key' in dict.keys ( )

JnanadaOptions: AD
Aug 18, 2022

A. 'key' in dict D. 'key' in dict.keys ( )

Dav023Options: AB
Oct 6, 2022

A, B and D are rights!

jaimebbOptions: BD
Nov 12, 2022

The only one that not works it is C, all the others works correctly.

mlsc01Options: AD
Feb 22, 2023

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')

Acid_ScorpionOptions: AD
Sep 25, 2023

A - is correct D - is correct B is can't be correct, as it checks value, not key

OracleistOptions: AD
Feb 6, 2024

A,D C is not working at all B is checking if the value of key is different than None

MokelOptions: AD
Apr 30, 2022

AD is the correct answer

bebiOptions: AD
Jul 1, 2022

A and D are the correct answers. B needs try/except.

PremJaguar
Jul 18, 2022

the print statement looks like PYTHON 2!!!

NetspudOptions: AD
Feb 14, 2023

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)

kosa997
Jul 27, 2023

A is wrong - this takes the value, doesn't check if key exists