I1 · Python Programming for AI

Tuples and Sets

A tuple is an ordered collection that cannot be changed after it is created, written in round brackets, such as (12.97, 77.59). A set is an unordered collection with no duplicate items, written in curly brackets, such as {"red", "blue"}, and supports operations such as union and intersection.

8 min Intermediate Jump to the activity

What you need: The earlier topics of this module.

Definition

How do you find the unique words in a story?

A tuple is an ordered collection that cannot be changed after it is created, written in round brackets, such as (12.97, 77.59). A set is an unordered collection with no duplicate items, written in curly brackets, such as {"red", "blue"}, and supports operations such as union and intersection.

In simple words

Use a tuple for things that belong together and shouldn't change, such as a point's latitude and longitude or a date. Use a set when you only care whether something is present: putting all the words of a story into a set removes repeats instantly.

Why it matters

Choosing the right collection makes code clearer and faster. Checking whether an item is in a set is far faster than searching a long list, which matters when you handle vocabularies of thousands of words.

How it works

listtupleset
Brackets[ ]( ){ }
OrderedYesYesNo
ChangeableYesNoAdd and remove only
DuplicatesAllowedAllowedRemoved

Sets support maths-like operations: a | b (union, everything in either), a & b (intersection, only what is in both) and a - b (difference).

In code

A tuple for a fixed location, and sets for unique and shared words. Sets have no order, so they are sorted before printing.

unique_words.py
location = (18.52, 73.86)      # latitude, longitude of Pune
lat, lon = location
print("Latitude:", lat, "Longitude:", lon)

story = "the cat sat on the mat and the cat slept"
words = story.split()
unique = set(words)
print("Words:", len(words), "Unique:", len(unique))
print("Unique words:", sorted(unique))

mine = {"python", "maths", "art"}
yours = {"python", "music", "maths"}
print("Both like:", sorted(mine & yours))
print("Either likes:", sorted(mine | yours))
print("Is 'art' in mine?", "art" in mine)

Output

Latitude: 18.52 Longitude: 73.86
Words: 10 Unique: 7
Unique words: ['and', 'cat', 'mat', 'on', 'sat', 'slept', 'the']
Both like: ['maths', 'python']
Either likes: ['art', 'maths', 'music', 'python']
Is 'art' in mine? True

Try it yourself

List, tuple or set?

Which collection fits each job best?

  • Every different word used in a book

  • Today's to-do items, which you'll add to and tick off

  • The (red, green, blue) values of one fixed colour

  • Usernames already taken, checked often

  • Marks in the order students sat the test

Common mistakes

  • Writing (5) for a one-item tuple. It needs a comma: (5,).
  • Expecting a set to keep order or allow duplicates.
  • Using {} for an empty set. That's an empty dictionary; use set().

Check yourself

Question 1

1. What does set([1, 1, 2]) contain?

Question 2

2. Can you change a tuple after creating it?

Question 3

3. {1, 2, 3} & {2, 3, 4} is…

Summary

  • Tuples are ordered and fixed; use them for values that belong together.
  • Sets are unordered with no duplicates; great for unique items and fast checks.
  • Union |, intersection & and difference - work on sets.

Finished reading?

Sources: Python docs: Data Structures