Loading the lesson
Loading the lesson
Forward Deployed Engineer · Programming Foundations · Lesson 6 of 10
Lists, dictionaries, sets and tuples, and how to choose between them.
Read the short notes and pictures.Do everything inside a blue frame.
list
[70, 85, 92]Ordered. You can change it. Duplicates allowed.
tuple
(12.97, 77.59)Ordered. Fixed once made.
set
{"ml", "ai"}No duplicates. No order.
dict
{"name": "Asha"}Each key points to a value.
Positions start at 0, so the first item is [0].
1scores = [70, 85]2scores.append(92)3first = scores[0]4scores[1] = 885print(len(scores))
Press Next to run the first line.
Memory
Nothing yet
Output
A list finds things by position. A dictionary finds them by name.
1user = {"name": "Asha", "city": "Pune"}2user["age"] = 283city = user["city"]4for key, value in user.items():5print(key, value)
Press Next to run the first line.
Memory
Nothing yet
Output
1tags = ["ml", "ai", "ml", "data", "ai"]2unique = set(tags)3print(len(unique))4print("ai" in unique)
Press Next to run the first line.
Memory
Nothing yet
Output
Real data is usually a list of dictionaries: many records, each with the same keys. This is the shape you will see in almost every API response.
1users = [2{"name": "Asha", "score": 82},3{"name": "Ravi", "score": 67},4]5passed = []6for u in users:7if u["score"] >= 70:8passed.append(u["name"])9print(passed)
Press Next to run the first line.
Memory
Nothing yet
Output
Count how many times each word appears in words = ["ai", "ml", "ai", "data", "ai"]. Store the counts in a dictionary.
Hint: counts.get(word, 0) gives the count so far, or 0 the first time.
You need the price of a product, given its name. Which one fits?
You have 10,000 email addresses with repeats and want each one once. Which one fits?
A list of dicts is how most real data arrives. To summarise it, walk through it once and keep track of what you have seen so far.
1readings = [2{"city": "Pune", "temp": 27.4},3{"city": "Delhi", "temp": 31.0},4{"city": "Shimla", "temp": 12.5},5]6warmest = readings[0]7total = 08for r in readings:9if r["temp"] > warmest["temp"]:10warmest = r11total = total + r["temp"]12print(warmest["city"], round(total / len(readings), 1))
Press Next to run the first line.
Memory
Nothing yet
Output
Using the same readings list, find the coldest reading and print its city.
Hint: Same shape: start with readings[0], and swap when the temperature is lower.
Practice: lists and dicts
Store the name of the person with the highest score in best.
Press Run to see the output here.
A list
for score in [70, 85, 92]:
print(score)
for i, score in enumerate([70, 85]):
print(i, score)A dictionary
prices = {"tea": 20, "coffee": 40}
for item, price in prices.items():
print(item, price)A list comprehension builds a new list from an old one. It reads like a sentence: this value, for each item, if a condition holds.
With a loop
passed = []
for s in scores:
if s >= 50:
passed.append(s)As a comprehension
passed = [s for s in scores if s >= 50]
Given words = ["Pune", "goa", "Delhi"], make a list of the names in lower case using a comprehension.
Keep these