remove last element from dictionary python 1

remove last element from dictionary python

D={1:'a',2:'b',3:'c'}
x=D.popitem()
print(D,x)
>>>{1:'a',2:'b'} (3,'c')
#x is the removed item, D is the rest of the dict

Here is what the above code is Doing:
1. Create a dictionary with three items.
2. Remove the last item from the dictionary and store it in x.
3. Print the dictionary and the removed item.

The popitem() method removes the last item from the dictionary.

Similar Posts