Skip to course content
Free course

Python Foundations / Module 3 / Why Collections Matter

Module 3 lesson

Why Collections Matter

Unit ID: M03-U00 Estimated active time: 18-25 minutes

One value is not always enough

So far, one name has usually referred to one number, boolean, text value, or None. Real tasks often need several related values.

Imagine three kinds of information:

  1. A sequence of module titles in learning order.
  2. One course record with labelled fields such as title, hours, and status.
  3. A group of unique skill tags where duplicates do not matter.

Python provides different collections because these shapes are different.

Four core collection types

TypeMain strengthExample use
listOrdered, changeable sequenceModule titles in course order
tupleOrdered, fixed groupingA coordinate or fixed version record
dictValues stored under meaningful keysOne labelled course record
setUnique items and fast membership questionsUnique tags or categories

The best structure depends on the task, not personal habit.

Look at the shapes

module_titles = ["Values", "Collections", "Control flow"]
version = (1, 0)
course = {"title": "Python Foundations", "hours": 32}
skill_tags = {"python", "data", "beginner"}

Notice the delimiters:

  • lists use square brackets;
  • tuples usually use parentheses;
  • dictionaries use braces with key-value pairs;
  • sets use braces with individual values.

An empty pair of braces {} creates an empty dictionary, not an empty set. Use set() for an empty set.

Pause and choose

Choose a structure for each need:

  • the first, second, and third assessment attempts;
  • a record containing a learner-safe fictional course code, title, and duration;
  • the unique file types used by a project;
  • a fixed red-green-blue colour triple.

Reasonable choices are a list, dictionary, set, and tuple. Other choices may work, but you should explain what meaning they preserve.

Collections also have types

print(type(module_titles))
print(type(version))
print(type(course))
print(type(skill_tags))

As before, type affects the available operations.

Takeaway

Collections represent shapes of related information. Choose by order, labels, uniqueness, and whether change is expected. Next, we will begin with the most common ordered, changeable collection: the list.