Command Palette

Search for a command to run...

Hectal
Phase 4Beginner5 of 18 in Python

Lists, Tuples, Dictionaries and Sets

Lists, tuples, dictionaries and sets: how each works inside, how to copy them safely, the collections toolbox, and how to choose the right one.

So far each variable has held one value. Real programs hold many: the items in a cart, the scores of a class, the settings of an app, the messages of a chat. This phase teaches the four built-in containers that Python gives you for that: the list, the tuple, the dictionary and the set.

You will learn how to use each one, and also how each one works inside: why a dictionary finds a key almost instantly, why a list cannot be a dictionary key, why copying a list does not always copy what is in it. These ideas explain most of the surprising bugs beginners meet, and they are the same ideas interviewers ask about.

The phase ends with comprehensions (a short way to build containers), nested data (the JSON-shaped data that every web API and AI model API returns), the collections toolbox, and a plain-words guide to what each operation costs, so you can choose the right container on purpose.

0/11 · 0%
11 topics ~5 h 32 code blocks & diagrams
Start with the first topic
1
4.1

Lists: Ordered, Changeable Collections

A list holds many values in a fixed order, and you can add, remove and change items after it is created. Each item has a position number (an index) starting at 0, and a list stores references to its items, not copies of them.

28 min 3 code practice

2
4.2

Slicing, Sorting and Searching Lists

A slice items[start:stop:step] takes a part of a list as a new list, and you can also assign to a slice to change many items at once. sorted() returns a new sorted list, sort() reorders in place, and key= chooses what to sort by.

28 min 4 code practice

3
4.3

Tuples and Unpacking

A tuple is an ordered collection that cannot be changed after it is created, written with round brackets. Tuples are the natural way to bundle a few related values, and unpacking lets you split a tuple (or any sequence) into separate variables in one line.

27 min 2 code practice

4
4.4

Dictionaries: Look Things Up by Key

A dictionary stores pairs: each key leads to a value, and you look a value up by its key instead of by position. Dictionaries keep the order in which keys were added (guaranteed since Python 3.7) and are the shape of almost all JSON data.

27 min 3 code practice

5
4.5

How Dictionaries Work: Hashing

A dictionary turns each key into a number called a hash, and uses that number to jump straight to a slot in a table, so lookup takes about the same time however big the dictionary is. This is why keys must be hashable (unchanging) and why a list cannot be a key.

28 min 4 code practice

6
4.6

Sets: Unique Items and Fast Membership

A set is a collection of unique items with no duplicates and no guaranteed order, built on the same hashing as dictionaries. Sets answer "is this in the collection?" very fast and support union, intersection and difference like in school maths.

25 min 2 code practice

7
4.7

List, Dict and Set Comprehensions

A comprehension builds a new list, dictionary or set from another collection in one readable line: what to produce, where to take items from, and an optional filter. It replaces the common loop-and-append pattern, as long as it stays short enough to read at a glance.

29 min 3 code practice

8
4.8

Nested Data: Lists of Dicts and Friends

Real data is nested: a list of dictionaries whose values are lists or more dictionaries. This topic shows how to read, search, update and print such JSON-shaped data safely, which is the shape of every web API and AI model response.

26 min 2 code practice

9
4.9

References, Shallow Copy and Deep Copy

Assigning a list or dictionary to another name does not copy it; both names point at the same object. A shallow copy makes a new outer container that still shares the inner objects, and a deep copy duplicates everything all the way down.

27 min 3 code practice

10
4.10

The collections Toolbox

The standard library module collections adds specialised containers: Counter for counting, defaultdict for grouping, deque for fast work at both ends, namedtuple for records, OrderedDict for order-aware dictionaries and ChainMap for layered lookups.

26 min 3 code practice

11
4.11

Choosing the Right Structure and Its Cost

Every container makes some operations cheap and others expensive. This topic explains Big-O in plain words, compares the cost of the common operations on lists, deques, sets and dictionaries, and gives a short guide for choosing on purpose.

28 min 1 diagram 2 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.