Sequences and dictionaries
Choose lists, tuples or dictionaries — and reach into nested data safely.
What you will be able to do
- Slice and unpack sequences
- Choose a tuple over a list
- Read and update dictionaries safely
Lists keep ordered, changeable values. Tuples are fixed records. Dictionaries map keys to values and are ideal for labelled data.
scores = [88, 92, 79, 95]
print(scores[0], scores[-1])
print(scores[1:3])
print(sorted(scores, reverse=True))
scores.append(81)
scores[1] = 94
print(scores)
point = (4, 7)
x, y = point
print(f"x={x}, y={y}")
learner = {"name": "Ada", "scores": scores}
learner["passed"] = True
print(learner.get("email", "no email"))
print(learner["name"], len(learner["scores"]))| Structure | Ordered | Changeable | Use it for |
|---|---|---|---|
| list | Yes | Yes | A growing sequence |
| tuple | Yes | No | A fixed record |
| dict | Insertion | Yes | Labelled fields |
- Slice
- A copy of part of a sequence, written start:stop.
- Unpack
- Assign several values in one statement.
- Key
- The unique identifier in a dictionary.
- get
- Read a key with a fallback instead of raising KeyError.
Your turn
MediumCount word frequencies
Count how often each word appears in the sentence and print the resulting dictionary.
PYTHON · starter
text = "python is readable and python is fast"
# your code hereCheck your understanding before moving on.
Knowledge checkQuestion 1
Which call returns a new sorted list and leaves the original untouched?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.