Start free

A level · 5 exercises

Python sets

A set holds each value once, in no particular order: set(list) drops repeats at once.

Checking whether a value is in a set is fast. & keeps what two sets share, | joins them, - keeps what is only in the first.

a = set(input().split())
b = set(input().split())
print(sorted(a & b))

Watch for: A set has no order: sort it (sorted()) before printing, or the order may change.

Exercises

  1. Different wordsHow many different words are in a line?Intermediate
  2. In both listsPrint the numbers that appear in both lists, sorted, each once.Intermediate
  3. The missing numberThe numbers from 1 to n are given in some order, but one is missing.Intermediate
  4. PangramA pangram uses every letter of the English alphabet at least once.Intermediate
  5. In just one listPrint the numbers that are in exactly one of the two lists, sorted.Intermediate
Start with “Different words”