0
0
Pythonprogramming~5 mins

sorted() function in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the sorted() function do in Python?
The sorted() function takes any iterable (like a list or tuple) and returns a new list with all the items in ascending order by default.
Click to reveal answer
beginner
How do you sort a list in descending order using sorted()?
Use the reverse=True argument inside sorted(). For example: sorted(my_list, reverse=True) sorts the list from highest to lowest.
Click to reveal answer
intermediate
What is the difference between sorted() and the list method sort()?
sorted() returns a new sorted list and works on any iterable. list.sort() changes the original list in place and returns None.
Click to reveal answer
intermediate
How can you sort a list of strings ignoring case using sorted()?
Use the key=str.lower argument to sort strings without case sensitivity. Example: sorted(words, key=str.lower).
Click to reveal answer
beginner
What type of object does sorted() return?
sorted() always returns a new list, even if the input is a tuple or other iterable.
Click to reveal answer
What will sorted([3, 1, 2]) return?
A[1, 2, 3]
B[3, 2, 1]
C[3, 1, 2]
DError
Which argument sorts a list in descending order?
Areverse=True
Bdescending=True
Corder='desc'
Ddesc=True
What does sorted() return when given a tuple?
AA sorted tuple
BA sorted list
CThe original tuple
DAn error
How do you sort strings ignoring case?
Asorted(strings, key=ignore_case)
Bsorted(strings, ignore_case=True)
Csorted(strings, key=str.lower)
Dsorted(strings, case=False)
Which is true about list.sort() compared to sorted()?
A<code>list.sort()</code> returns a tuple
B<code>list.sort()</code> returns a new sorted list
C<code>list.sort()</code> works on any iterable
D<code>list.sort()</code> sorts the list in place
Explain how to use the sorted() function to sort a list in descending order and ignoring case for strings.
Think about the arguments <code>reverse</code> and <code>key</code>.
You got /5 concepts.
    Describe the difference between sorted() and list.sort() in Python.
    Consider what happens to the original list.
    You got /4 concepts.