Skip to content

Instantly share code, notes, and snippets.

@hmasila
Created November 20, 2017 20:10
Show Gist options
  • Save hmasila/4ead1a1c2a450e136fc9671cd33ef48d to your computer and use it in GitHub Desktop.
Save hmasila/4ead1a1c2a450e136fc9671cd33ef48d to your computer and use it in GitHub Desktop.
sets created by andela_hmasila - https://repl.it/@andela_hmasila/sets
# Sets revisited in python
def union(list1, list2):
result = list(set(list1 + list2))
print("Union of {} and {} is {}".format(list1, list2, result))
def intersect(list1, list2):
result = [x for x in list1 if x in list2]
print("Intersection of {} and {} is {}".format(list1, list2, result))
def difference(list1, list2):
result = [x for x in list1 if x not in list2]
print("Difference of {} and {} is {}".format(list1, list2, result))
def cross_product(list1, list2):
result = [(x * y) for x in list1 for y in list2]
print("Cross product of {} and {} is {}".format(list1, list2, result))
def equality(list1, list2):
result = list1 == list2
print("Equality of {} and {} is {}".format(list1, list2, result))
def subset(list1, list2):
result = set(list1) < set(list2)
print("Subset of {} and {} is {}".format(list1, list2, result))
def proper_subset(list1, list2):
result = set(list1) < set(list2) and list1 != list2
print("Proper subset of {} and {} is {}".format(list1, list2, result))
def complement(list1, list2):
result = [x for x in list2 if x not in list1]
print("Complement of {} and {} is {}".format(list1, list2, result))
complement([1,2,3,4], [1,4,2,6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment