dictionary pop() function in python by R4R Team

- pop() in a dictionary slightly same as popitem() function, both are able to pop(delete) the item from the dictionary.
- popitem() function remove the top most item of the dictionary.
- while pop() function remove the specified item from the dictionary.

Syntax-

dict.pop(key)

Example-

dictionary is d={1:"one",2:"two",3:"three"}
d.pop(1)
will give {2:"two",3:"three"}


program-

d={1:"one",2:"two",3:"Three",4:"Four"}
print(d)

d.pop(1)
print(d)

d.pop(2)
print(d)

d.pop(3)
print(d)

d.pop(4)
print(d)


output-

{1: 'one', 2: 'two', 3: 'Three', 4: 'Four'}
{2: 'two', 3: 'Three', 4: 'Four'}
{3: 'Three', 4: 'Four'}
{4: 'Four'}
{}


-In this program, initially we have a dictionary {1:"one",2:"two",3:"three"} then we pop the items from the dictionary with the help of the keys like d.pop(1), here 1 is a key so this function remove the content of 1.
-If we do not give any argument to pop() function like-


program-

d={1:"one",2:"two",3:"Three",4:"Four"}
print(d)
d.pop()
print(d)


output-

d.pop()
TypeError: pop expected at least 1 arguments, got 0




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!