Back to Freecodecamp

Comparing and Sorting Tuples

curriculum/challenges/english/blocks/python-for-everybody/5e7b9f0b0b6c005b0e76f06d.md

latest730 B
Original Source

--description--

More resources:

- <a href="https://www.youtube.com/watch?v=EhQxwzyT16E" target="_blank" rel="noopener noreferrer nofollow">Exercise</a>

--questions--

--text--

Which does the same thing as the following code?:

python
lst = []
for key, val in counts.items():
    newtup = (val, key)
    lst.append(newtup)
lst = sorted(lst, reverse=True)
print(lst)

--answers--

python
print( sorted( [ (v,k) for k,v in counts.items() ], reverse=True ) )

python
print( [ (k,v) for k,v in counts.items().sorted() ] )

python
print( sorted( [ (v,k) for k,v in counts.keys() ] ) )

python
print( [ (k,v) for k,v in counts.values().sort() ] )

--video-solution--

1