List Pop() Method In Python, How To Use?-Kaizensk

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_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.

 



Source link