List Methods Part 3: Organization & Order
Last list method lesson, I promise! If you are feeling overwhelmed, don't worry—these methods will become easier and easier as you use them. Soon, they will be tools that you reach for naturally whenever you need them.
The first method we are going to learn about today is .sort().
The .sort() Method: Lining Up Alphabetically
Imagine your movie theater line has become a mess. To make things organized, the manager asks everyone to line up in alphabetical order.
In Python, .sort() does exactly this. It look at the items in your list and rearranges them automatically.
How it works with Strings:
If you have a list of letters or names, .sort() puts them in A-B-C order.
- Print the ordered list.
How it works with Numbers:
If your list contains numbers, .sort() will arrange them from smallest to largest (ascending order).
- Print the ordered list.
.sort() vs. sorted()
The method vs the function.
In our movie theater analogy, .sort() is like the manager telling everyone in the actual line to move around and reorganize. But sorted() is like the manager taking a photo of the line and then rearranging the people in the photo, while the real people stay exactly where they were.
- The .sort() Method (The "Mover")
- In-place: It physically changes the original list.
- Return Value: It returns None.
- The sorted() Function (The "Photographer")
- Not in-place: It leaves the original list alone.
- Return Value: It creates a brand new sorted list that you can save to a variable.
- Print numbers.
- Print new_numbers.
When to use which?
| Feature | .sort() | sorted() |
|---|---|---|
| Original List | Changed forever. | Stays the same. |
| Result | Returns None | Returns a new sorted list. |
| Storage | Use when you don't need the old order anymore. | Use when you want to keep the original order for later. |
The .reverse() Method
Finally, we have .reverse(). This is different from sorting. It doesn't care about the alphabet or numbers; it simply flips the list upside down. The person at the back is now at the front.
- Print line.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named list_methods.py in your part24 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Alphabetical Order
The movie theater manager wants the guest list organized alphabetically to make check-in easier.
-
Starting List:
guests = ['Zelda', 'Adam', 'Charlie', 'Bob'] -
Task: Use the
.sort()method to arrange the names in A-Z order. - Goal: Print the
guestslist.
Exercise 2: The "Safe" Sort (sorted)
You want to see what the list would look like sorted, but you need to keep the original arrival order for your records.
-
Starting List:
arrival_order = ['Zelda', 'Adam', 'Charlie', 'Bob'] -
Task: Use the
sorted()function to create a new list calledorganized_guests. -
Goal: Print both
arrival_order(should be unchanged) andorganized_guests(should be sorted).
Exercise 3: Reverse Alphabetical
The theater decides to board the plane from Z to A.
-
Starting List:
guests = ['Zelda', 'Adam', 'Charlie', 'Bob'] -
Task: Use
.sort()with thereverse=Trueargument inside the parentheses. - Goal: Print the list to see it ordered from Z to A.
Exercise 4: Flip the Line
The manager announces that the person at the back of the line is now the first person to enter!
-
Starting List:
line = ['Alice', 'Bob', 'Charlie', 'Dave'] -
Task: Use the
.reverse()method (Note: this is NOT the same as a reverse sort). -
Goal: Print the list. It should show
['Dave', 'Charlie', 'Bob', 'Alice'].
Exercise 5: The "None" Trap
This exercise demonstrates a common mistake students make with in-place methods.
-
Starting List:
numbers = [5, 2, 9, 1] -
Task: Run this specific code:
result = numbers.sort(). -
Goal: Print
result. Observe that it saysNone. In a comment, explain whyresultis empty butnumbersis sorted.
Don't Forget to commit and Push!