Lesson Β· β± 7 min read
π§Ί Lists and dictionaries: storing many values
EnglishHinglish
π Real-life example
A shopping list is ordered: first milk, then bread, then eggs. That's a Python list. Your phone's contact book links a name to a number: 'Amma β 98xxxx'. You look things up by name, not by position. That's a dictionary.
π‘ In simple words
Real data comes in groups, so Python has collections. A list [ ] keeps items in order and you access them by position, starting from 0. A dictionary { } stores key β value pairs and you look up values by key, which is very fast. A tuple ( ) is like a list that can't be changed, and a set keeps only unique items. Most data you'll see in AI work, such as JSON from APIs, is lists of dictionaries.
πͺ Step by step
- List: an ordered collection you can change. scores = [72, 88, 95]; scores.append(60); scores[0] is the first item.
- Dictionary: look things up by a key. emp = {"name": "Ravi", "city": "Pune"}; emp["city"] gives "Pune". Use emp.get("age", 0) when a key may be missing.
- Set: unique values with fast membership checks. seen = set(); if user_id in seen: ... Great for removing duplicates.
- Loop over them: for k, v in emp.items(): print(k, v). Most real data is a list of dictionaries, like rows from an API.
- Build new lists quickly with comprehensions: high = [s for s in scores if s > 80].
π Key terms
- List
- An ordered collection: ['milk', 'bread'].
- Dictionary
- Key β value pairs: {'name': 'Asha', 'city': 'Pune'}.
- Index
- The position of an item in a list. The first item is index 0.
π See it in action
cart = ["milk", "bread", "eggs"]
print(cart[0]) # milk
cart.append("tea")
prices = {"milk": 30, "bread": 45, "eggs": 72, "tea": 120}
total = sum(prices[item] for item in cart)
print(total) # 267A list for what's in the cart, and a dictionary to look up each price.
β οΈ Common mistakes to avoid
- Searching a big list with 'in' inside a loop. It gets very slow; convert to a set first.
- KeyError from emp["age"] when the key doesn't exist. Use .get() or check 'if "age" in emp'.
- Changing a list while looping over it. Build a new list instead.
β Quick check
You want to look up a student's marks by their roll number. Which is best?
- A list
- A single string
- A dictionary
Show answer
C. A dictionary: A dictionary maps a key (roll number) straight to a value (marks), with no searching through positions.
π Questions learners ask
List or dictionary: how do I choose?
If you access items by position or just go through them in order, use a list. If you look items up by a name or ID, use a dictionary.
What is JSON and how does it relate?
JSON is the text format APIs use. It maps directly to Python dictionaries and lists: json.loads(text) gives you dicts and lists, json.dumps(data) turns them back into text.
π Remember
List = ordered line of items. Dictionary = look up by name (key).
Learn this as part of a full course
Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:
AI Foundations Path Β· AI Product Engineer Β· LLM / Agent Engineer Β· AI Forward Deployed Engineer
Continue in the course