Skip to content
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

Use 'key' kwarg in min/max/sorted when calculating with dicts #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/1/calculating_with_dictionaries/example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# example.py
#
# Example of calculating with dictionaries
from operator import itemgetter

prices = {
'ACME': 45.23,
Expand All @@ -11,15 +12,15 @@
}

# Find min and max price
min_price = min(zip(prices.values(), prices.keys()))
max_price = max(zip(prices.values(), prices.keys()))
min_price = min(prices.items(), key=itemgetter(1))
max_price = max(prices.items(), key=itemgetter(1))

print('min price:', min_price)
print('max price:', max_price)

print('sorted prices:')
prices_sorted = sorted(zip(prices.values(), prices.keys()))
for price, name in prices_sorted:
prices_sorted = sorted(prices.items(), key=itemgetter(1))
for name, price in prices_sorted:
print(' ', name, price)