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/a5d79f64310b3074f102079ec17e3508 to your computer and use it in GitHub Desktop.
Save eder-projetos-dev/a5d79f64310b3074f102079ec17e3508 to your computer and use it in GitHub Desktop.
Python - List comprehensions
# Traditional approach
squares = []
for num in range(1, 11):
squares.append(num**2)
# List comprehension
squares = [num**2 for num in range(1, 11)]
@eder-projetos-dev
Copy link
Author

11 Tricks That Will Make Your Life Easier as a Python Developer

List comprehensions are a powerful tool in Python, and I believe they are essential to master. They allow you to express complex operations on lists in a concise and expressive manner. Whenever you find yourself iterating over a list to perform a certain operation and appending the results to a new list, consider using a list comprehension instead. It will not only save you lines of code but also make your intentions clear to other developers who read your code.

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