From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Built-ins

Built-ins - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

Built-ins

- [Presenter] Let's say that we have a list of tasks. Each task is a tuple, where the first field is the task name and the second field is the priority. You can see it in our code in builtin.py at line 14. We'd like to solve the tasks by priority. In Python, tuples are sorted in lexicographical order, which means if we'll sort as-is, we'll get the tasks sorted by their name. Tasks names are strings, and the string '3' is bigger than string "200". Let's see an example. "iphython" and when we do three bigger than 200, we get True. Python provides a "key" argument to sort. Key is a function that gets the object we'd like to compare and returns a value representing this object. For example, if we have names equal "tweety" and "bugs" and "daffy" and "elmer", if we'll do sorted of names, we'll get them sorted by lexicographical order. However, we can do sorted names and the key equal len, and then we'll get the names sorted by their length. We can use that to sort our tasks. In line 5 we…

Contents