16
Lesson 7 of 44

Tuples and sets

Discover Python tuples and sets, two data structures that complement lists. You will learn how to group values that must not change, how to unpack them, how to remove duplicates and how to compare collections with set operations such as union, intersection and difference. Reinforce what you learn with practical exercises and learn to choose the right structure for each problem.

  • 5 theory articles
  • 11 practice exercises
Start lesson

Fundamentals concepts 5

Tuples: data that does not change

A tuple is an ordered sequence of values, like a list, but with a key difference: once created it cannot be modified . It is the…

Theory
Read more

Sets: values without repeats

A set is a collection of unique, unordered values. If you add the same value twice, only one is kept. It is the perfect structure…

Theory
Read more

Set operations

Python sets work like sets in mathematics: you can join them, intersect them or subtract them. It is the shortest and most…

Theory
Read more

Choosing the structure and common mistakes

Python has three basic collections to store several values: list, tuple and set. Choosing the right one makes the code clearer…

Theory
Read more

Unpacking, named tuples and frozenset

Tuples shine when combined with unpacking : spreading the elements of a sequence into variables in a single line.

Theory
Read more

Practice exercises 11

Point on a plane

Create a Python program that stores the coordinates of a point in a tuple and unpacks them to show them.

Exercise Easy
Solve it

Minimum and maximum with a tuple

Create a Python program with a function that returns the minimum and the maximum of a list of numbers at once.

Exercise Easy
Solve it

Remove duplicates

Create a Python program that removes the repeated values from a series of numbers using a set.

Exercise Easy
Solve it

Common elements

Create a Python program that shows the numbers that appear in two lists at once, using the intersection of sets.

Exercise Medium
Solve it

Distinct letters

Create a Python program that counts how many different letters a text has.

Exercise Easy
Solve it

Same letters

Create a Python program that checks whether two words are made of the same distinct letters, regardless of the order or how many…

Exercise Medium
Solve it

Guest lists

Create a Python program that compares the guest lists of two parties using set operations.

Exercise Medium
Solve it

Student ranking

Create a Python program that sorts some students by grade using a list of tuples.

Exercise Medium
Solve it

Points with named tuples

A named tuple ( namedtuple ) is a tuple whose fields have names: you access them with p.x instead of p[0] , and it is still…

Exercise Medium
Solve it

Words with the same letters

Sets cannot be used as dictionary keys, but frozenset (immutable sets) can. They serve to group by “which letters appear”…

Exercise Hard
Solve it

Unpacking with a star

Extended unpacking first, *middle, last = data gathers all the leftover elements in a list. It is the most readable way to…

Exercise Medium
Solve it