Addaly is in open beta. Things will change, and AI answers can be wrong — check anything that matters.

Python, From Zero, For AI

From your first line of code to your first API call.

Lesson 11 of 897 min

Lists and dicts, and knowing which one you need

A list is things in a row

python
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))    # 625

Square 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:

python
print(prices[9])
IndexError: list index out of range

A 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

python
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:

python
print(student["marks_2"])
KeyError: 'marks_2'

Safer, when you are not sure:

python
print(student.get("marks_2"))          # None
print(student.get("marks_2", 0))       # 0
print("city" in student)               # True

A key names one slot

This matters more than it looks:

python
votes = {"yes": 3, "no": 1, "yes": 5}
print(votes)        # {'yes': 5, 'no': 1}
print(len(votes))   # 2

Writing "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.

Positions, or named slotslist — prices = [120, 90, 200]Fetched by position: prices[2]Order is part of the meaningDuplicates are normal and keptA position that does not exist raisesIndexErrorRight for: twelve months of rainfall, everyturn in a conversationdict — student = {"name": "Asha", …}Fetched by key: student["city"]You never look things up by positionOne key names one slot, so writing twicereplacesA key that does not exist raises KeyError, or.get gives a defaultRight for: one student's name, city and marksThe honest answer to which one is usually both, nested: a list of turns, each turn a dict with a roleand a content. That is the exact shape you will send to a model API, and you can already build it.
Positions, or named slotslist — prices = [120, 90, 200]Fetched by position: prices[2]Order is part of the meaningDuplicates are normal and keptA position that does not exist raisesIndexErrorRight for: twelve months of rainfall, everyturn in a conversationdict — student = {"name": "Asha", …}Fetched by key: student["city"]You never look things up by positionOne key names one slot, so writing twicereplacesA key that does not exist raises KeyError,or .get gives a defaultRight for: one student's name, city andmarksThe honest answer to which one is usually both,nested: a list of turns, each turn a dict with arole and a content. That is the exact shape you willsend to a model API, and you can already build it.

The honest answer to "which one" is usually: both, nested. This is the shape you will meet everywhere in AI work:

python
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"])          # user

A 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

python
messages.append({"role": "assistant", "content": "गुरुत्वाकर्षण..."})
print(len(messages))   # 4

When 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

python
basket = [
    {"item": "rice", "price": 850, "qty": 2},
    {"item": "oil", "price": 1200, "qty": 1},
]
print(basket[1]["item"])
basket[0]["qty"] = 3
print(basket[0])

The one thing to keep

A list is positions in order; a dict is named slots, and one name means exactly one slot.

Before you move on

You run `votes = {"yes": 3, "no": 1, "yes": 5}` and then `print(len(votes))` and `print(votes["yes"])`. What do you get?

Pick the one you would defend. Nobody sees your answer.

No ads. No data sale. No public scores on people. Ever.

© 2026 Addaly