Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 996 Bytes

set.md

File metadata and controls

42 lines (34 loc) · 996 Bytes
layout
default

Lists

Official Documentation

  • Sets do not preserve order
  • Sets cannot contain multiple instances of the same element
  • Checking if an element occurs in the set is very efficient

Creation

# Literal
s = set()          # Empty set
s = { }            # Empty dictionary!!
s = { 1, 2, 3, 4 }

Set Comprehension

s = { x**2 for x in range(1, 5) }
# => { 1, 4, 9, 16 }

Operations

Syntax Semantics
x in s Check if x occurs in s
`s1 s2`
s1 - s2 Difference
s1 & s2 Intersection
s.add(x) Add x to s
s1 < s2 Check if s1 a strict subset of s2
s1 <= s2 Check if s1 a subset of s2
s1 > s2 Check if s1 a strict superset of s2
s1 >= s2 Check if s1 a superset of s2
s.pop() Removes and returns a random element from s
s.pop(x) Removes x from s