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 eder-projetos-dev/521c78e6e295eb6a7a636c14c09bb443 to your computer and use it in GitHub Desktop.
Save eder-projetos-dev/521c78e6e295eb6a7a636c14c09bb443 to your computer and use it in GitHub Desktop.
Python - Set comprehensions
even_numbers = {num for num in range(1, 11) if num % 2 == 0}
@eder-projetos-dev
Copy link
Author

Set Comprehensions

I find set comprehensions useful when I need to create a set containing unique elements based on some condition. For example, if I want to create a set of even numbers from 1 to 10, I can use a set comprehension along with a conditional statement to filter out the odd numbers.

https://levelup.gitconnected.com/11-tricks-that-will-make-your-life-easier-as-a-python-developer-da29e4306675

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment