KnowledgeBoat Logo
|

Informatics Practices

Assertion (A): Consider the code given below:

d={1: 'Amit', 2: 'Sumit', 5:'Kavita'}
d.pop(1) 
print(d) 

The output will be:

Amit #item is returned after deletion
{2: 'Sumit', 5: 'Kavita'}

Reasoning (R): pop() method not only deletes the item from the dictionary but also returns the deleted value.

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true but R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.

Python Dictionaries

1 Like

Answer

Both A and R are true and R is the correct explanation of A.

Explanation
In the given code, when d.pop(1) is called, it removes the key 1 and returns the corresponding value 'Amit'. The subsequent print(d) statement displays the updated dictionary without the key-value pair (1, 'Amit'). The pop() method in Python dictionaries not only deletes the specified key-value pair from the dictionary but also returns the value associated with the deleted key.

Answered By

2 Likes


Related Questions