30.5k views
2 votes
The _________ method returns a randomly selected key-value pair from a dictionary.

rand_pop()
popitem()
pop()
random()

User Imjosh
by
7.5k points

1 Answer

2 votes

Final answer:

The popitem() method returns a randomly selected key-value pair from a dictionary.

Step-by-step explanation:

The popitem() method returns a randomly selected key-value pair from a dictionary. This method removes the selected key-value pair from the dictionary. Here's an example:

import random

my_dict = {1: 'apple', 2: 'banana', 3: 'orange'}

random_item = my_dict.popitem()

print(random_item) # Output: (3, 'orange')

The popitem() method returns a randomly selected key-value pair from a dictionary in Python. Unlike pop(), which removes a specified key and returns the corresponding value, popitem() removes and returns a random key-value pair. The other options provided, such as rand_pop() and random(), are not standard dictionary methods in Python.

User Lenora
by
6.9k points