Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Create sort_dict_by_value.py #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

ashishmeshram844
Copy link
Contributor

Write a program to sort dictionary as per values

  • In this program first we define a dictionary with key value pair which are unsorted order
  • used a sorted() function for sorting dictionary as per values and create a new sorted dictionary

sort a given dictionary according to values
@lostxmusafir
Copy link

Define an unsorted dictionary

unsorted_dict = {
'apple': 3,
'banana': 1,
'cherry': 5,
'date': 2,
'elderberry': 4
}

Sort the dictionary by its values and create a new sorted dictionary

sorted_dict = dict(sorted(unsorted_dict.items(), key=lambda item: item[1]))

Print the sorted dictionary

print("Sorted Dictionary:", sorted_dict)

Copy link

@lostxmusafir lostxmusafir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define an unsorted dictionary

unsorted_dict = {
'apple': 3,
'banana': 1,
'cherry': 5,
'date': 2,
'elderberry': 4
}

Sort the dictionary by its values and create a new sorted dictionary

sorted_dict = dict(sorted(unsorted_dict.items(), key=lambda item: item[1]))

Print the sorted dictionary

print("Sorted Dictionary:", sorted_dict)

@learning-zone learning-zone merged commit a28d9b2 into learning-zone:master Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.