After this lesson, you will be able to: Use lists, tuples, dictionaries, and sets to organize and manipulate collections of data.
Real programs almost always work with collections, a list of users, a record of stats, a set of unique tags. This lesson covers Python's four core data structures, when to use each one, and the most common operations on them.
Lists hold any number of items in order. Use square brackets and access items by index (starting at 0).
fruits = ["apple", "banana", "cherry"]print(fruits[0]) # appleprint(len(fruits)) # 3fruits.append("mango") # add to endfruits[1] = "blueberry" # change itemfruits.remove("cherry") # remove by valuefor fruit in fruits:print(fruit)
Dictionaries pair keys with values. Use them when you want to look something up by name, not position.
student = {"name": "Alex","age": 17,"grades": [88, 92, 79]}print(student["name"]) # Alexstudent["age"] = 18 # updatestudent["school"] = "BiTree HS" # add new keyfor key, value in student.items():print(key, "=", value)
Diagram coming soon!
Two columns labeled "Key" and "Value" in a dictionary diagram, name → Alex, age → 18, grades → [88, 92, 79]
Tuples are like lists but unchangeable, once created, you can't modify them. They use parentheses: point = (10, 20). Sets are unordered collections with no duplicates: tags = {"python", "web", "python"} becomes {"python", "web"}. Use sets when you only care about uniqueness or membership tests.
Build a tiny phonebook in the editor. Add three contacts, then look one up. The lookup name "Sam" is pre-loaded as input. Pass once your code prints the right number and uses a dictionary plus the `in` keyword for the lookup.
Pick the one that automatically removes duplicates.
Sign in and purchase access to unlock this lesson.