PythonLesson 11 of 16
Tuples and sets in Python
A tuple is a list you can't change: coordinates, a date, a pair of values. A set keeps only unique items and quickly finds what two groups share.
Key points
- (25.2, 55.3) is a tuple; its items can't be replaced
- lat, lon = point unpacks a tuple into variables
- set(my_list) drops duplicates
- a | b is the union, a & b the overlap, a - b the difference
- Sets have no order, so use sorted() before printing
Example
point = (25.2, 55.3)lat, lon = pointprint("Lat:", lat, "Lon:", lon) tags = ["python", "code", "python", "fun"]unique = set(tags)print(len(tags), len(unique))print(sorted(unique))print("code" in unique)The tag list has four items but only three are unique: the second python is gone. sorted() turns the set into an ordered list.
Task
How to use the editor⌘ Enter or Ctrl Enter runs the code, ⇧ ⌘ Enter checks the task.Tab and Shift Tab indent the line or the selection; Esc, then Tab leaves the editor.The check compares what the program printed with the task; spaces at line ends do not matter.After two failed checks you may open the solution. Your code is kept in this browser.Two parties, two guest lists. Find everyone who came at least once (everyone) and those who came on both days (both).
Output
What your program prints appears here.