DeveloperBreeze

Sorting a Dictionary by Values

python
# Define a dictionary
my_dict = {'apple': 3, 'banana': 1, 'cherry': 2}

# Sort the dictionary by values using a lambda function
sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))

Continue Reading

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!