Question: Here is a dict of pets and how many of them.

```
{"Birds" : 2, "Cats" : 3, "Dogs" : 4}

```

I want to print the items sorted by largest number of the same pet to the smallest number of the same pet, or vice versa.

Desired output:

```
('Dogs', 4)
('Cats', 3)
('Birds', 2)

```

And

```
('Birds', 2)
('Cats', 3)
('Dogs', 4)

```

How could this be done?

I do not want to import anything.

Answer: 
with lambda expression `x[1]` stands for values, `x[0]` for keys in key-value pair from a dictionary.

```
sorted(d.items(), key=lambda x: x[1], reverse=True) 
sorted(d.items(), key=lambda x: x[0])

```

without lambda expr, operator.itemgetter does the same thing:

```
from operator import itemgetter

sorted(d.items(), key=itemgetter(1), reverse=True)
sorted(d.items(), key=itemgetter(0))

```
Using the built-in `sorted` function with a special ranking function and a `reverse=True` argument:

```
for item in sorted(dict.items(), key=lambda x: x[1], reverse=True):
    print(item)

```

A key of `lambda x: -x[1]` should also work (without reverse argument).