← Python for Beginners
0 of 27 lessons done0%

Collections · Lesson 19 of 27

Tuples and sets

Tuples

A tuple is like a list that can't be changed. It uses round brackets.

point = (3, 4)
print(point[0])    # 3
x, y = point       # "unpacking": x = 3, y = 4

Use tuples for values that belong together and shouldn't change, like coordinates.

Sets

A set stores only unique values, and it has no order.

nums = {1, 2, 2, 3, 3, 3}
print(nums)          # {1, 2, 3}
nums.add(4)
print(3 in nums)     # True, and very fast even for big sets

Turning a list into a set is the quickest way to remove duplicates: set([1, 1, 2]) → {1, 2}.

Since sets have no order, use sorted() to get a sorted list from one.

Your task

Read a line of words. Print:

  1. how many unique words there are
  2. the unique words in alphabetical order, separated by spaces

For red blue red green blue:

3
blue green red