A list is things in a row
prices = [120, 340, 90]
print(prices[0]) # 120
print(prices[2]) # 90
print(len(prices)) # 3
prices.append(75)
print(prices) # [120, 340, 90, 75]
print(sum(prices)) # 625Square brackets, commas between items. Positions start at zero, so the third item is prices[2]. This feels wrong for about a week and then feels normal.
Ask for a position that does not exist and Python stops:
print(prices[9])IndexError: list index out of rangeA list keeps its order, allows duplicates, and can be changed after it is made. prices[0] = 130 replaces the first item.
A dict is things with labels
student = {"name": "Amara", "city": "Kano", "marks": 78}
print(student["city"]) # Kano
student["marks"] = 81 # change one
student["year"] = 2 # add a new one
print(student)Curly braces, and each entry is a key and a value separated by a colon. You look things up by key, not by position. student[0] does not work here, because there is no position zero; there is a slot called "name".
Ask for a key that is not there and Python stops:
print(student["marks_2"])KeyError: 'marks_2'Safer, when you are not sure:
print(student.get("marks_2")) # None
print(student.get("marks_2", 0)) # 0
print("city" in student) # TrueA key names one slot
This matters more than it looks:
votes = {"yes": 3, "no": 1, "yes": 5}
print(votes) # {'yes': 5, 'no': 1}
print(len(votes)) # 2Writing "yes" twice did not store two entries and did not raise an error. The second write landed in the same slot and replaced what was there. A dict is a set of named boxes, and a name refers to exactly one box.
Choosing between them
Use a list when the items are the same kind of thing and their order or count matters. Four prices. Twelve months of rainfall. Every message in a conversation.
Use a dict when each piece has a different job and you will fetch it by name. One student's name, city and marks. One product's title, price and stock.
The honest answer to "which one" is usually: both, nested. This is the shape you will meet everywhere in AI work:
messages = [
{"role": "user", "content": "Explain gravity in one sentence."},
{"role": "assistant", "content": "Things with mass pull on each other."},
{"role": "user", "content": "Now in Hindi."},
]
print(len(messages)) # 3
print(messages[0]["content"]) # Explain gravity in one sentence.
print(messages[-1]["role"]) # userA list, because the order of a conversation is the whole point and there can be any number of turns. Dicts inside it, because a turn has two named parts. Read messages[0]["content"] left to right: take the list, take item zero, that is a dict, take its "content" slot.
messages[-1] is the last item. Negative positions count from the end, which saves you writing messages[len(messages) - 1].
Adding a turn
messages.append({"role": "assistant", "content": "गुरुत्वाकर्षण..."})
print(len(messages)) # 4When you call an AI API in lesson ten, you will send almost exactly this structure. You already know how to build it.
Try this now
basket = [
{"item": "rice", "price": 850, "qty": 2},
{"item": "oil", "price": 1200, "qty": 1},
]
print(basket[1]["item"])
basket[0]["qty"] = 3
print(basket[0])Before you move on