Skip to content

Instantly share code, notes, and snippets.

@gcrsaldanha
Created June 27, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcrsaldanha/9e800051432d79f775485712d9f5985e to your computer and use it in GitHub Desktop.
Save gcrsaldanha/9e800051432d79f775485712d9f5985e to your computer and use it in GitHub Desktop.
This gist demonstrates the usage of `set()` and `{}` for building sets in python and when one is preferred over the other
import dis
def non_empty_set_literal():
return {1, 2, 3} # Build a set {1, 2, 3}
def non_empty_set_method():
return set([1, 2, 3]) # Builds a set {1, 2, 3} from a list
def empty_set_literal():
return {} # Does NOT work => Generates an empty dict {}
def empty_set_method():
return set() # Generates an empty set
dis.dis(non_empty_set_literal)
dis.dis(non_empty_set_method)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment