Skip to content

Instantly share code, notes, and snippets.

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 DugalMcCrow/cff86648e8c36c47ebbe9054d4acf3e6 to your computer and use it in GitHub Desktop.
Save DugalMcCrow/cff86648e8c36c47ebbe9054d4acf3e6 to your computer and use it in GitHub Desktop.
>>> # Dictionary comprehension
>>> squares_dict = {number: number*number for number in numbers}
>>> squares_dict
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}
>>>
>>> # Set comprehension
>>> numbers_dups = [1, 2, 3, 4, 3, 2, 1]
>>> squares_set = {number*number for number in numbers_dups}
>>> squares_set
{16, 1, 4, 9}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment