Python Sets

Sets are unordered collections of unique elements. Use curly braces {} or the set() function. They don't allow duplicate value

1. Creating Sets:

my_set = {1, 2, 3} # Using curly braces, like dictionaries
my_set = set([1,2,3,2]) #Using set() function: duplicate value (2) automatically eliminated/ignored/removed on conversion where function input uses a valid `list` here with a repeated item as provided by its initializer directly which in other contexts like simply a local variable itself assigned and populated in-place using `[]` notation does retain any duplications that may exist from data assigned given scope etcetera where `set()` conversion implies uniqueness property print (my_set) #Output:{1,2,3}. Even though '2' is repeated within `list` variable before implicit type-casting here, 2 retained unlike cases where only one occurrence or usage at one step required, such as when adding to dict/set later where no duplicates would occur due to use rather than syntax. In the example the conversion using set() occurs and only after explicit attempt when using the print function

2. Adding and Removing items:

my_set= {1,2,3}
my_set.add(4) #add element to set
print(my_set) # Output:{1, 2, 3, 4}
my_set.remove(2) #Removes 2 if found; raises KeyError if not
print(my_set) #Output: {1, 3, 4}
my_set.discard(3) # Removes 3, without checking or error otherwise
my_set.discard(10) # discarding an element which is not present won't raise any error.
my_set.clear() #removes all the items form set make a set empty

3. Set operations

Sets offer the ability to manipulate groups of distinct/unique element-wise occurrences using standard set terminology and functionality related thereby rather than requiring use of indices etc. which only ever occur when sequence information explicitly available such as in arrays or lists which by design unlike Set itself retain sequence data too and may end up differing despite the exact elements used due to such positional metadata (unlike a set defined by distinct individual element-wise items rather than sequential arrangement thereof such as when defining in some order, or checking existence where an item must be found as in a list which despite any sorting would then preserve positions of those unique occurrences assuming unique data types, if so, while in a Set all order disregarded when looking since it could or would exist more or less by default within ordered context like List etc. without explicit enforcement You compare like items if suitable for purpose when deciding or similar such application of Boolean tests which differ notably from numeric sort used implicitly since otherwise there are potentially ambiguous behaviour if only testing where a sort assumes ordering at once unless type distinctions available at time. Set ops provide alternative when unique rather than ordering itself relevant as when merging datasets with and without shared items or columns given shared name when using some mapping method if available when testing since no ordering required only knowledge or verification of item occurrences rather than indices to specify what item is handled rather than relying exclusively on element name if it repeats within data and type changes apply for that data after updates/sorting to preserve some invariant such as positional sorting)

Method Description Example
union() Returns new set containing elements or members from both input sets even if similar or exact in value type, assuming type consistency applicable/appropriate
set1 = {1, 2, 3}
set2 = {3, 4, 5}

set3 = set1.union(set2) #Union
print(set3) #Output: {1, 2, 3, 4, 5}
intersection() returns elements shared in both sets if found, otherwise, intersection result empty because no matches, i.e., completely disjoint so you could perform checks using that to filter which further checks need occur if a step produces empty value assuming no intersection case irrelevant rather than causing crashes at a point like invalid indexing operations assuming such indices exist as could otherwise check to verify when required to specify how a non-intersecting sequence-style variable treated without that indice to test
set1 = {1,2,3}
set2 = {3,4,5}
set3 = set1.intersection(set2)
print(set3) # Output:{3} because sets here using identical values of some int for simplicity/illustrative purposes, as opposed to testing on some key to ensure shared occurrences despite distinct objects such as rows in same table with same primary or index field where those fields not involved, only other values being validated for intersecting element conditions, such as filtering table one for some field based on other matching field column criteria being applied over contents or intersection values generated with set/dict when filtering records assuming they exist based upon criteria
difference() Returns new set consisting elements found in set1 but not in set2. Set members used directly in value tests
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1.difference(set2)
print(set3) # Output: {1, 2}
symmetric_difference() new set, items exclusive to one or the other. Items shared between those being checked omitted in result since assumed these values are in some sense common already as in union method although this symmetric set differs notably if empty where union won't reduce this far if types or variable itself still in scope regardless matching content whereas symmetric comparison only looks for the unique-valued elements within individual Sets themselves at time. Such testing could in effect apply as filter like checking two table headers, by generating such result in-place, thereby identifying unshared row indices and filtering these from later intersection given field or columns if that makes sense here unlike simpler examples operating on similar or identical field type data in those prior Sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1.symmetric_difference(set2)
print(set3) # Output: {1, 2, 4, 5}