Exam PCAP All QuestionsBrowse all questions from this 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.)

    Correct Answer: A, D

    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
efemonaOptions: AD

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

Bubu3kOptions: AD

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

rotimislawOptions: 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

ciccio_benzinaOptions: AD

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

OracleistOptions: AD

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

Acid_ScorpionOptions: AD

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

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

jaimebbOptions: BD

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

Dav023Options: AB

A, B and D are rights!

JnanadaOptions: AD

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

macxszOptions: AD

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

kosa997

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

NetspudOptions: 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)

PremJaguar

the print statement looks like PYTHON 2!!!

bebiOptions: AD

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

MokelOptions: AD

AD is the correct answer