TIME NOW CALENDAR CONVERT LENGTH CONVERT TEMPERATURE DICTIONARIES, LISTS SCIENCE EDUCATION RELIGION WORK CALCULATOR CALCULATE LIFE

Python 3 programming language, learn python, tutorial

ALL TOPICS

Python 3. Sets

A set object is an unordered collection of immutable values. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. set:  mutable, unique, unordered. frozenset- Immutable, unique, unordered.

Sets vs lists: In practical applications, lists are very nice to sort and have order while sets are nice to use when you don't want duplicates and don't care about order. https://stackoverflow.com/questions/12354515/what-is-the-difference-between-sets-and-lists-in-python. Sets and frozensets are hashable. Hashing is a concept in computer science which is used to create high performance, pseudo random access data structures where large amount of data is to be stored and accessed quickly.

Set represents a collection of distinct elements. In python, sets are mainly used for two reasons (Book: Data Science from Scratch, Joel Gruce):
• For faster operation: in is a very fast operation on sets. If we have a large collection of elements and if we wish to perform membership test, in that case it is appropriate to use set instead of a list.
• To find a distinct items in a collections. Programmers use sets much less frequently than dicts and lists.
1. Sets
# set do not have duplicates
# Output: {1, 2, 3, 4}
my_set = {1,2,3,4,3,2}
print(my_set)

# set cannot have mutable items
# here [3, 4] is a mutable list
# If you uncomment line #12,
# this will cause an error.
# TypeError: unhashable type: 'list'

#my_set = {1, 2, [3, 4]}

# we can make set from a list
# Output: {1, 2, 3}
my_set = set([1,2,3,2])
print(my_set)

2. Intersection. New set with elements common to X and Y:

X.intersection(Y)
X&Y

Union. New set with elements from both X and Y:
X|Y
X.union(Y)

Difference.  New set with elements in X but not in Y:
s.difference(t)
X-Y

Superset:
X>Y

Cardinality of set X:
len(X)

Test X for membership in Y:
X in Y

Test X for non-membership in Y:
X not in Y

s <= t    test whether every element in s is in t :
s.issubset(t)

s >= t    test whether every element in t is in   s:
s.issuperset(t)

s ^ t    new set with elements in either s or t but not both
s.symmetric_difference(t)

s.copy()        new set with a shallow copy of s

String to set, equal elements. All exist:
set("spam") == set ("maps")

Example:
X = {1,2,3,4,3,2}
Y = {1,2}

print (X|Y)

print (X.union(Y))

3.
# initialize a with {}
a = {}

# check data type of a
# Output: <class 'dict'>
print(type(a))

# initialize a with set()
a = set()

# check data type of a
# Output: <class 'set'>
print(type(a))

4.
# initialize my_set
my_set = {1,3}
print(my_set)

# if you uncomment line 9,
# you will get an error
# TypeError: 'set' object does not support indexing

#my_set[0]

# Output: {1, 2, 3}
print(my_set)

# Output: {1, 2, 3, 4}
my_set.update([2,3,4])
print(my_set)

# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4,5], {1,6,8})
print(my_set)

5. For sets and frozen sets:
 len(s) cardinality of set s x in s test x for membership in s x not in s test x for non-membership in s s.issubset(t) s <= t test whether every element in s is in t s.issuperset(t) s >= t test whether every element in t is in s s.union(t) s | t new set with elements from both s and t s.intersection(t) s & t new set with elements common to s and t s.difference(t) s - t new set with elements in s but not in t s.symmetric_difference(t) s ^ t new set with elements in either s or t but not both s.copy() new set with a shallow copy of s

6. Operations only for sets:

 s.update(t) s |= t update set s, adding elements from t s.intersection_update(t) s &= t update set s, keeping only elements found in both s and t s.difference_update(t) s -= t update set s, removing elements found in t s.symmetric_difference_update(t) s ^= t update set s, keeping only elements found in either s or t but not in both s.add(x) add element x to set s s.remove(x) remove x from set s; raises KeyError if not present s.discard(x) removes x from set s if present s.pop() remove and return an arbitrary element from s; raises KeyError if empty s.clear() remove all elements from set s

---
ALL TOPICS