Python · Level 2 Data

Sets

Remove duplicates and find what two collections have in common with Python sets.

  • set() keeps each item once
  • & finds items in both sets
  • | combines, - subtracts

A set keeps each item only once and has no order. & keeps what is in both sets, | combines them.

a = set(["py", "web", "py"])   # {'py', 'web'}
b = {"web", "db"}
print(a & b)                   # {'web'}
print(sorted(a | b))           # ['db', 'py', 'web']
print(sorted(a - b))           # ['py']

Write common_tags(a, b) that returns the tags found in both lists, each once, as a sorted list.

solution.py
Test Suites Pending
Stdout