3.10 Lists
- Lists
- Popcorn Hack 1: Lists and Indexing
- Code Runner Challenge
- Popcorn Hack 2: Sum and Average with Accumulator Pattern
- Code Runner Challenge
- Popcorn Hack 3: Frequency Counting and Filtering
- Code Runner Challenge
- Homework: Complete Dataset Analysis
- Code Runner Challenge
Lists
Popcorn Hack 1: Lists and Indexing
Code Runner Challenge
Lists Popcorn 1 - predict, run, fix the crash
View IPYNB Source
# CODE_RUNNER: Lists Popcorn 1 - predict, run, fix the crash
items_needed = ["blankets", "socks", "water", "meals"]
print(items_needed[0]) # Prediction: blankets
print(items_needed[len(items_needed) - 1]) # Prediction: meals
print(len(items_needed)) # Prediction: 4
print(items_needed[3]) # Prediction: items_needed[4] raises IndexError (last index is 3). Fixed to [3] -> meals
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 2: Sum and Average with Accumulator Pattern
Code Runner Challenge
Lists Popcorn 2
View IPYNB Source
# CODE_RUNNER: Lists Popcorn 2
volunteer_hours = [7, 6, 8, 5, 7, 9]
if len(volunteer_hours) == 0:
print("No data yet")
else:
total = 0
for hours in volunteer_hours:
total += hours
print(f"Sum: {total}")
average = total / len(volunteer_hours)
print(f"Average: {average}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 3: Frequency Counting and Filtering
Code Runner Challenge
Lists Popcorn 3 - count then filter
View IPYNB Source
# CODE_RUNNER: Lists Popcorn 3 - count then filter
items_requested = ["blankets", "socks", "meals", "socks", "water", "socks", "meals"]
frequency = {}
for item in items_requested:
if item in frequency:
frequency[item] += 1
else:
frequency[item] = 1
popular = []
for item in frequency:
if frequency[item] >= 2:
popular.append(item)
print(frequency)
print(popular)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework: Complete Dataset Analysis
Code Runner Challenge
Lists Homework - indexing, accumulator, count and filter on one dataset
View IPYNB Source
# CODE_RUNNER: Lists Homework - indexing, accumulator, count and filter on one dataset
donations_by_item = ["blankets", "socks", "blankets", "water", "socks", "blankets",
"meals", "socks", "blankets", "water", "blankets", "socks"]
costs = [2, 3, 2, 4, 3, 2, 1, 3, 2, 4, 2, 3]
print(len(donations_by_item))
print(donations_by_item[-1])
total = 0
for cost in costs:
total += cost
average = total / len(costs)
print(f"Total: {int(total)}")
print(f"Average: {int(average)}")
frequency = {}
for item in donations_by_item:
if item in frequency:
frequency[item] += 1
else:
frequency[item] = 1
print(frequency)
popular = []
for item in frequency:
if frequency[item] >= 3:
popular.append(item)
print(popular)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...