teluzia.Cloud Engineer · Day 23
CHAPTER 04 · PYTHON FUNDAMENTALS

Lists, dictionaries, sets and tuples

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Choose a collection based on its contract: sequence, mapping, unique membership or fixed grouping. You will represent a small service inventory with built-in structures and deliberately request a missing dictionary key. The repair should preserve the intended meaning of missing data. Lists are ordered mutable sequences that can contain repeated values. Index zero selects the first item, negative indexes count from the end, and slicing creates selected subsequences. append mutates a list and returns None rather than the new list. An index outside the current length raises IndexError. Dictionaries map hashable keys to values and preserve insertion order. Direct indexing requires the key to exist, whereas get can provide an intentional default. Values may be nested structures. Distinguish an absent key from a present key whose value is None; validation should decide which states are permitted.

Think of it this way

A list is an ordered queue, a dictionary is a labeled cabinet, a set is a membership roster, and a tuple is a fixed bundle.

TOPIC 01 / 03Step by step

Lists and indexing

Lists are ordered mutable sequences that can contain repeated values. Index zero selects the first item, negative indexes count from the end, and slicing creates selected subsequences. append mutates a list and returns None rather than the new list. An index outside the current length raises IndexError. services=["api","worker"]; services.append("api"); print(services[0],services[-1],len(services)) prints api api 3 because duplicates are preserved and append changed the existing list. Sequence semantics matter when order and repeated observations are meaningful, such as request events or deployment steps, and distinguish mutable updates from methods that return new collections to a caller explicitly. Assigning services=services.append("db") replaces your variable with None; call append separately and inspect the original list instead of assuming every mutation returns the updated object for further use.

WORKED EXAMPLE
services=["api","worker"]; services.append("api"); print(services[0],services[-1],len(services)) prints api api 3 because duplicates are preserved and append changed the existing list.
Why this matters

Sequence semantics matter when order and repeated observations are meaningful, such as request events or deployment steps, and distinguish mutable updates from methods that return new collections to a caller explicitly.

A common trap

Assigning services=services.append("db") replaces your variable with None; call append separately and inspect the original list instead of assuming every mutation returns the updated object for further use.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Lists and indexing

Lists are ordered mutable sequences that can contain repeated values. Index zero selects the first item, negative indexes count from the end, and slicing creates selected subsequences. append mutates a list and returns None rather than the new list. An index outside the current length raises IndexError. services=["api","worker"]; services.append("api"); print(services[0],services[-1],len(services)) prints api api 3 because duplicates are preserved and append changed the existing list. Sequence semantics matter when order and repeated observations are meaningful, such as request events or deployment steps, and distinguish mutable updates from methods that return new collections to a caller explicitly. Assigning services=services.append("db") replaces your variable with None; call append separately and inspect the original list instead of assuming every mutation returns the updated object for further use.

WORKED EXAMPLE
services=["api","worker"]; services.append("api"); print(services[0],services[-1],len(services)) prints api api 3 because duplicates are preserved and append changed the existing list.

Why this matters

Sequence semantics matter when order and repeated observations are meaningful, such as request events or deployment steps, and distinguish mutable updates from methods that return new collections to a caller explicitly.

A common trap

Assigning services=services.append("db") replaces your variable with None; call append separately and inspect the original list instead of assuming every mutation returns the updated object for further use.

Dictionaries and missing keys

Dictionaries map hashable keys to values and preserve insertion order. Direct indexing requires the key to exist, whereas get can provide an intentional default. Values may be nested structures. Distinguish an absent key from a present key whose value is None; validation should decide which states are permitted. ports={"api":8080}; ports["db"] raises KeyError, while ports.get("db",5432) supplies an explicit fallback chosen by the program’s configuration policy rather than by Python automatically. Mapping semantics make named configuration readable and allow validation to distinguish required fields from optional defaults, preventing silent substitution of a value that might connect automation to an unintended destination service. Using a default for every missing key can conceal broken required configuration; validate mandatory fields and fail clearly rather than treating absence as acceptable for all settings in production automation scripts.

WORKED EXAMPLE
ports={"api":8080}; ports["db"] raises KeyError, while ports.get("db",5432) supplies an explicit fallback chosen by the program’s configuration policy rather than by Python automatically.

Why this matters

Mapping semantics make named configuration readable and allow validation to distinguish required fields from optional defaults, preventing silent substitution of a value that might connect automation to an unintended destination service.

A common trap

Using a default for every missing key can conceal broken required configuration; validate mandatory fields and fail clearly rather than treating absence as acceptable for all settings in production automation scripts.

Sets and tuples

Sets represent unique hashable members and support intersection, union and difference; they do not promise the sequence ordering needed for a workflow. Tuples are immutable sequences, useful for fixed groupings, although contained mutable objects can still change. Collection choice should reflect semantics, not merely convenient syntax. set(["api","worker","api"]) has two members; ("api",8080) groups a service and port. Sort a set when you need deterministic display order for output or tests. Choosing the right collection exposes intent and avoids accidental deduplication, unpredictable displayed order or unnecessary mutation when representing inventory, relationships and structured results in a small automation program written by a learner. Converting an event list to a set loses repetitions and order; do so only when unique membership is the desired information, and do not describe every tuple as deeply immutable when it contains mutable values.

WORKED EXAMPLE
set(["api","worker","api"]) has two members; ("api",8080) groups a service and port. Sort a set when you need deterministic display order for output or tests.

Why this matters

Choosing the right collection exposes intent and avoids accidental deduplication, unpredictable displayed order or unnecessary mutation when representing inventory, relationships and structured results in a small automation program written by a learner.

A common trap

Converting an event list to a set loses repetitions and order; do so only when unique membership is the desired information, and do not describe every tuple as deeply immutable when it contains mutable values.

KEEP THESE WITH YOU

Today’s takeaways

Use lists for ordered repeated data.

Validate required mapping keys.

Use sets for membership and tuples for fixed grouping.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed