Skip to content

Instantly share code, notes, and snippets.

@lfalanga
Last active February 7, 2020 01:51
Show Gist options
  • Save lfalanga/f5c9f052a707bb8037afa13c1916e12d to your computer and use it in GitHub Desktop.
Save lfalanga/f5c9f052a707bb8037afa13c1916e12d to your computer and use it in GitHub Desktop.
# Creating set
F = {1, 2, 3, 4, 5, 3}
print(F) # {1, 2, 3, 4, 5}
print(len(F)) # 5
# Creating an empty set
F3 = set()
# Creating Intersection
print(F & F1) # {1, 2, 3, 4}
# Creating Union
print(F | F1) # {1, 2, 3, 4, 5, 10, 13, 15}
# Creating Difference
print(F - F1) # {5}
# Comparing two sets sizes
print(F1 > F2) # True
# Removing duplicates from a list
L = [1, 2, 1, 3, 2, 4, 5]
print(L) # [1, 2, 1, 3, 2, 4, 5]
L = list(set(L))
print(L) # [1, 2, 3, 4, 5]
# Example 2: Using intersection function
socios = ["Juan", "Pedro", "Susana", "Anna", "Sofía", "Pablo"]
ajedrez = ["Pedro", "Susana", "Anna", "Sofía", "Pablo"]
natacion = ["Juan", "Pedro", "Susana"]
resultado = set(ajedrez).intersection(natacion)
print(resultado) # {'Pedro', 'Susana'}
print(type(resultado)) # <class 'set'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment