Skip to content

GCI12000 [Team TREE][2025] - For loop vs list comprehension #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

echeniquegrecia
Copy link

When you use a for loop, on every iteration, you have to look up the variable holding
the list and then call its append() function. A list comprehension can be faster
than a for loop because it’s optimized for performance by Python’s internal mechanisms.

References:

@echeniquegrecia echeniquegrecia force-pushed the test branch 2 times, most recently from 86b0d88 to c116177 Compare May 21, 2025 08:51
@MP-Aubay MP-Aubay changed the title Team TREE 2025 - For loop vs list comprehension GCI12000 [Team TREE][2025] - For loop vs list comprehension May 21, 2025
MP-Aubay
MP-Aubay previously approved these changes May 21, 2025
Copy link
Contributor

@MP-Aubay MP-Aubay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me

@MP-Aubay MP-Aubay added the 👀 👀 review done 👀 👀 review done - waiting for changes label May 21, 2025
"creedengo",
"eco-design",
"performance",
"comprehension",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please delete tags "comprehension", "loop", and "iteration" because there aren't thematic tags but code centric

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks !

@@ -0,0 +1,18 @@
{
"title": "To use list comprehension instead for loop in simple iterations",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete the "To" please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks !

@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added rule GCI1200: Detect a for loop and suggest a list comprehension
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add here a link to current PR like done in other example in CHANGELOG.md

@@ -83,6 +83,7 @@ Some are applicable for different technologies.
| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
| GSI1200 | To use list comprehension instead for loop in simple iterations | | | ❓ | ❓ | 🚀 | ❓ | ❓ |❓ |❓ |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo error : GCI and not GSI, please

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

Successfully merging this pull request may close these issues.

3 participants