Python lists and dictionaries for beginners
Lists keep items in order. Dictionaries connect a key to a value. Learn the difference, avoid common mistakes, and combine both structures in a small inventory project you can run in Python 3.
When should you use a list?
Use a list when order matters or when you want to work through a sequence of items. Positions start at zero.
topics = ["variables", "loops", "functions"]
print(topics[0]) # variables
topics.append("lists")
print(len(topics)) # 4
topics[0] reads the first item. append adds one item to the end. Trying to read a position that does not exist raises IndexError.
When should you use a dictionary?
Use a dictionary when each value has a meaningful label. Keys must be unique.
progress = {
"variables": 100,
"loops": 70,
"functions": 30
}
print(progress["loops"]) # 70
progress["lists"] = 10
print(progress.get("classes", 0)) # 0
Square brackets are useful when the key must exist. get is safer when a key may be missing because you can provide a default value.
List or dictionary: a quick rule
- Use a list for ordered steps, names, scores or tasks.
- Use a dictionary for a profile, settings, counts or values looked up by name.
- Use a list of dictionaries when you have several records with the same fields.
learners = [
{"name": "Asha", "xp": 120},
{"name": "Kabir", "xp": 85}
]
for learner in learners:
print(learner["name"], learner["xp"])
Mini-project: a tiny learning inventory
This program counts completed practice items by topic. It uses a list for attempts and a dictionary for totals.
attempts = ["loops", "lists", "loops", "functions", "lists", "loops"]
totals = {}
for topic in attempts:
totals[topic] = totals.get(topic, 0) + 1
for topic, count in sorted(totals.items()):
print(f"{topic}: {count}")
Expected output:
functions: 1
lists: 2
loops: 3
The project is deliberately small: it counts exact text labels and does not correct spelling or understand synonyms. That limitation is useful to notice before calling a program “intelligent.”
Common beginner mistakes
1. Using parentheses instead of square brackets
# Wrong: topics(0)
# Correct:
topics[0]
2. Accessing a missing dictionary key
# May raise KeyError:
progress["classes"]
# Safe fallback:
progress.get("classes", 0)
3. Changing a list while looping over it
Removing items from the same list you are iterating can skip values. Build a new list with a comprehension instead.
scores = [42, 88, 61, 95]
passing = [score for score in scores if score >= 60]
print(passing) # [88, 61, 95]
Five practice exercises
- Create a list containing three AI topics and print the final item.
- Add
"dictionaries"to a list without replacing existing items. - Create a dictionary with your study minutes for Python and maths.
- Use
getto read a missing"ml"key with a default of zero. - Count the words in
["ai", "python", "ai", "data"]using a dictionary.
Show worked answers
# 1
topics = ["Python", "data", "machine learning"]
print(topics[-1])
# 2
topics.append("dictionaries")
# 3
minutes = {"python": 30, "maths": 20}
# 4
print(minutes.get("ml", 0))
# 5
words = ["ai", "python", "ai", "data"]
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)
What should you learn next?
Practice Python loops to process collections, then learn functions and return values so you can reuse your inventory logic. When you are comfortable, try the rule-based chatbot project.
Browse every free Lumio lesson or explore the AI engineering roadmap.