Python for data

Lesson 5 of 15

Lesson ยท 15 min

Sort and rank

Sort a list

sorted takes a list and gives a new list in order. The first list does not change. Numbers go from small to large, and text goes in alphabetical order.

print(sorted([34, 6, 20]))                 # [6, 20, 34]
print(sorted([34, 6, 20], reverse=True))   # [34, 20, 6]

Sort rows by a column

Python cannot compare two dictionaries, so sorted(trips) stops with an error. You tell Python which value to compare. The parameter key takes a function. Python calls that function with each row, and it sorts the rows by the values that the function returns.

def minutes_of(trip):
    return trip["minutes"]


by_minutes = sorted(trips, key=minutes_of)

Write the name of the function without parentheses. You do not call the function. Python calls it.

lambda: a function in one line

People often write such a small function in place, with the word lambda. This line does the same as the code above.

by_minutes = sorted(trips, key=lambda trip: trip["minutes"])

lambda trip: trip["minutes"] is a function with no name. trip is its parameter. It returns the value after the colon.

The top of the list

Sort from large to small, and take a slice.

longest_first = sorted(trips, key=lambda trip: trip["minutes"], reverse=True)
top5 = longest_first[:5]

When you need only one row, use max or min. They take the same key.

longest = max(trips, key=lambda trip: trip["minutes"])

max and min stop with an error when the list is empty.

Rows with the same value

Rows with the same key stay in the order that they had. Often you want a second rule for them. Let the key function return two values in parentheses. This is a tuple. Python compares the first values. When they are equal, it compares the second values.

by_rider = sorted(trips, key=lambda trip: (trip["rider"], trip["minutes"]))

reverse=True turns the whole order around. Sometimes you want a number from large to small, and a text from A to Z. Put a minus sign in front of the number.

ranked = sorted(trips, key=lambda trip: (-trip["minutes"], trip["start"]))

Your turn

main.py loads the 14 trips of trips.csv. Do the tasks in order.

Tasks

5 to do

  1. Task 1, not checked yet. Make a list by_minutes with all the trips, sorted by minutes from the shortest to the longest.

  2. Task 2, not checked yet. Make a list top3 with the 3 longest trips. The longest trip is first.

  3. Task 3, not checked yet. Make a variable shortest. It is the dictionary of the trip with the fewest minutes. Use min with a key.

  4. Task 4, not checked yet. Write a function rank(trips). It returns the trips sorted by minutes, from the longest to the shortest. Trips with the same minutes are sorted by start, from A to Z.

  5. Task 5, not checked yet. Write a function longest_from(trips, station). It returns the longest trip that starts at station. When no trip starts there, it returns None.

Task 1 of 5

Make a list by_minutes with all the trips, sorted by minutes from the shortest to the longest.

All tasks

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.