Created
June 3, 2023 15:18
-
-
Save eder-projetos-dev/a5d79f64310b3074f102079ec17e3508 to your computer and use it in GitHub Desktop.
Python - List comprehensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Traditional approach | |
squares = [] | |
for num in range(1, 11): | |
squares.append(num**2) | |
# List comprehension | |
squares = [num**2 for num in range(1, 11)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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