Episode 15 of 29

Sorting and Sets

Learn how to sort data and use sets for unique collections.

Python provides tools for sorting and unique collections.

Sorting Lists

nums = [3, 1, 4, 1, 5, 9, 2]
nums.sort()  # [1, 1, 2, 3, 4, 5, 9]
new_list = sorted(original)  # returns new list

Sets

Sets are unordered collections of unique elements:

colors = {"red", "green", "blue", "red"}
print(colors)  # {"red", "green", "blue"}

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b)   # Union
print(a & b)   # Intersection
print(a - b)   # Difference