Which built-in function can be used to sort a list?

Master Python with the PCAP Certification! Explore interactive quizzes and detailed explanations to ensure your exam success. Gain confidence and get certified!

The function that can be utilized to sort a list in Python is sorted(). This built-in function takes an iterable (like a list) as an argument and returns a new list containing all items from the iterable in ascending order.

sorted() is versatile because, unlike the method that modifies the list in place, it does not change the original list but instead produces a new sorted list. This can be particularly useful when you want to retain the original order of elements or when you are working with data that should remain unchanged after the sorting operation.

The other options, including sort(), order(), and arrange(), either do not exist in Python or serve different purposes. The sort() method is indeed applicable to lists, but it's important to note that it modifies the list in place rather than creating a new sorted list. Hence, while sort() is a valid way to sort a list when considering methods specific to list objects, sorted() is the correct answer as a general function applicable to any iterable.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy