Python Foundations·Shape the data

Sequences and dictionaries

Choose lists, tuples or dictionaries — and reach into nested data safely.

Beginner 14 min read Lesson 10 of 480 Challenge included
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.

sequences.pyPython
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"]))
StructureOrderedChangeableUse it for
listYesYesA growing sequence
tupleYesNoA fixed record
dictInsertionYesLabelled 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

Count word frequencies

Medium

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 here
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 10 of 480 · 0% of course