Legends ofPythos
Claim your name
Working with Data

Lists in depth

Lesson 1 of 14

Watch the lesson1:23 · with Torsten
Mental model: Think of a list as a tray holding distinct cards in order. Some tools let you grab all the cards and lay them out neatly on a new table (sorting), while others rearrange the cards right where they sit, changing that same tray.
Why it matters: Real programs constantly need to rank results. When you build a leaderboard for game scores or order sensor readings from coldest to hottest, these built-in tools do the heavy lifting so your logic stays clean and readable.

Sorting without side effects

sorted() returns a brand-new list in order, leaving the original untouched. The .sort() method rearranges your existing list right there and does not return anything new.
nums = [3, 1, 4]

# sorted() gives a NEW list; nums stays the same
new_nums = sorted(nums)
print(new_nums)   # [1, 3, 4]
print(nums)       # [3, 1, 4] (unchanged!)

nums.sort()
print(nums)        # [1, 3, 4] (modified in place)
The first call gave us a sorted copy while keeping the original order safe. The second method rearranged nums directly because we did not need the old sequence anymore.

Descending sort

scores = [85, 92, 78]

print(sorted(scores))                  # ascending by default: [78, 85, 92]
print(sorted(scores, reverse=True))   # descending:            [92, 85, 78]
The reverse flag flips the direction so the largest values appear first. This is exactly what you need when ranking scores from highest to lowest.

Built-in aggregations and lookups

temps = [41, 39, 52, 60]

print(min(temps))   # lowest value: 39
print(max(temps))   # highest value: 60
print(sum(temps))   # total: 192
min, max, and sum scan the list once to give you those three key statistics. You can compute them directly without writing a loop.
fruits = ['apple', 'banana', 'apple']
print(fruits.count('apple'))   # how many times it appears: 2
print(fruits.index('banana'))  # first position of that item: 1
count tells you frequency, while index finds the very first spot where a specific item lives in your sequence.

Copying a list safely

# Without a copy, both names point to the SAME object
a = [10, 20]
b = a          # b is just another name for a's list!

b.append(30)
print(a)       # [10, 20, 30] -- oops! we changed 'a' too.

# Make an independent copy instead
c = [40, 50]
d = c.copy()   # .copy() hands back a brand new list
d.append(60)
print(c)       # [40, 50] -- untouched
The first block shows that assigning one variable to another does not make a duplicate: both names point at the same list, so a change through either one shows up in both. The second block uses .copy() to build a genuinely separate list that you can modify without touching the original.

Your turn

0 of 3 solved

Exercise 1

+25 XP
Given the list of scores below, create a new variable called 'ranked' that holds the same numbers sorted in descending order. Do NOT modify the original 'scores' list.
scores = [72, 95, 81]

Run your code to check it against the tests.

Exercise 2

+25 XP
Using the 'temperatures' list, compute four values:
  • lowest – the minimum temperature
  • highest – the maximum temperature
  • total – the sum of all temperatures
  • avg – total divided by how many readings there are (use len())
Store each result in its own variable with exactly those names.
temperatures = [68, 72, 59, 81]

Run your code to check it against the tests.

Exercise 3

+25 XP
You have an inventory list and a customer's order.

Write code that does the following:
  1. Create remaining as a copy of inventory, then remove every item in order from it (use .remove() inside a loop). If an ordered item is not present, skip it silently by checking first with 'in'.
  2. Count how many times 'widget' appears in the original inventory and store that number in widgets_in_stock using .count().
  3. Find the index (position) of the FIRST occurrence of 'gadget' in remaining, if it exists; otherwise set its position to -1.
Store all three results in variables named exactly: remaining, widgets_in_stock, first_gadget_index.
inventory = ['widget', 'gizmo', 'widget', 'sprocket']
order     = ['widget', 'unknown_item', 'gizmo']

Run your code to check it against the tests.