Python Collections

Let’s look at collections

  1. Notice

    Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))
Live state

Top-level scope

items→Object 1
unique_items→Object 2
Object 1 · list['tea', 'tea', 'tea']Object 2 · set{'tea'}
This step

Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

Picture collections

Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

Image brief

Alt text
A diagram for Python Collections. Instructions: items = ["tea"] * 3 unique_items = set(items) print(len(items)) print(len(unique_items)). Displayed result: 3 1. The final example bindings are items --> object 1 (list); unique_items --> object 2 (set). object 1: ['tea', 'tea', 'tea']; object 2: {'tea'}
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Collections". Teaching focus: Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = ["tea"] * 3 unique_items = set(items) print(len(items)) print(len(unique_items)) | v DISPLAYED RESULT 3 1 FINAL BINDINGS items --> object 1 (list) unique_items --> object 2 (set) object 1: ['tea', 'tea', 'tea'] object 2: {'tea'} Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Collections. Instructions: items = ["tea"] * 3 unique_items = set(items) print(len(items)) print(len(unique_items)). Displayed result: 3 1. The final example bindings are items --> object 1 (list); unique_items --> object 2 (set). object 1: ['tea', 'tea', 'tea']; object 2: {'tea'}

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))
Live state

Top-level scope

items→Object 1
unique_items→Object 2
Object 1 · list['tea', 'tea', 'tea']Object 2 · set{'tea'}
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3
1

What do you expect this program to print?

What does this example display?

items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3 1

Follow the printed values in order. The output is: 3 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

Python Collections

In Python, collections are ways to store multiple values in a single variable. Imagine a box where you can keep many items together – that’s what collections help you do in programming.

Types of Collections

Python has four main types of collections:

  • List – an ordered, changeable collection
  • Tuple – an ordered, unchangeable collection
  • Set – an unordered, unique collection
  • Dictionary – a collection of key-value pairs

1. List – A Changeable Ordered Collection

Lists let you store multiple items in one variable. You can change, add, or remove items.

fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']

Why this output? The list keeps all three fruits in the order they were added. The square brackets [] show it’s a list.

2. Tuple – A Fixed Ordered Collection

Tuples are like lists, but you can’t change them once created.

colors = ("red", "green", "blue")
print(colors)
('red', 'green', 'blue')

Why this output? The items are inside parentheses (), showing it’s a tuple. The order is kept, but the values can’t be changed later.

3. Set – A Unique Unordered Collection

Sets store items without any duplicates and without keeping order.

numbers = {1, 2, 3, 2, 1}
print(numbers)
{1, 2, 3}

Why this output? Sets remove duplicates automatically. The numbers 2 and 1 appeared twice but are shown only once. The order may vary each time you run the code.

4. Dictionary – A Collection of Key-Value Pairs

Dictionaries store data in a key: value format. Each key is unique and helps you find its value.

student = {"name": "Alice", "age": 20}
print(student)
{'name': 'Alice', 'age': 20}

Why this output? The dictionary keeps two key-value pairs. The keys "name" and "age" point to their respective values.

Summary Table – Comparing Python Collections

Type Ordered? Changeable? Duplicates Allowed? Syntax
List Yes Yes Yes [ ]
Tuple Yes No Yes ( )
Set No Yes No { }
Dictionary No (but keys are read in insertion order) Yes No (keys must be unique) { key: value }

When to Use Each Collection?

  • Use a list when you need a collection that can change and has a specific order (e.g., a shopping list).
  • Use a tuple when the collection should never change (e.g., fixed coordinates).
  • Use a set when you only care about unique items (e.g., all unique tags from a blog).
  • Use a dictionary when you need to pair data (e.g., a person's name and age).

What to remember

Connect what you noticed in the example with the idea below.

Remember

Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.

Watch out for this idea

A set preserves duplicate elements.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Lists →