What is the primary function of the `sorted()` function?

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

The primary function of the sorted() function is to create a new sorted list from the elements of the original iterable. When you call sorted() with an iterable, it processes that iterable and returns a new list containing all the elements sorted in ascending order by default. This is beneficial because it does not modify the original iterable; instead, it produces a new list, allowing you to maintain the original order if needed. You can also customize the sorting order by providing additional parameters, such as a key function or a reverse flag, but its fundamental purpose is to return a sorted version of the input data.

The other options describe different functionalities that sorted() does not perform. Reversing the order of an iterable, appending items to a list, and removing duplicates are distinct operations that are handled by different functions or methods in Python.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy