What is pop() method in dictionary | How to use the pop() method of a dictionary.

Definition:

This method simply removes the specified from the original dictionary, also pop method returns the value so we have to assign it to some variable. This method changes the original dictionary.

Syntax:

X = dictionary.pop(‘key_name’ , ‘Default’)

key_name = Your desired key

Default = Default value if no key is present (Optional)

This method takes a single argument.

Working:

pop()_method_in_python

pop()_method_output

Copy code below.

dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Age’: 23,
‘Male’: True,
‘Subjects’: ‘Computers’}

x = dictionary.pop(‘Subjects’)
print(x)
print(dictionary)

Code:

  1. A dictionary with some items is being taken
  2. pop() method is being applied, and since it returns a value, the value is printed inside x.
  3. After the pop() method the original dictionary is printed.

Output

  1. The value inside x is printed.
  2. The original dictionary which was changed is printed.

Interview questions:

  1. Can you remove multiple items with the pop() method?

No, we can only remove a single item from the pop() method.

 

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *