Skip to content
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

[New Rule] if k in d: del d[k] #195

Open
InSyncWithFoo opened this issue Nov 25, 2024 · 0 comments
Open

[New Rule] if k in d: del d[k] #195

InSyncWithFoo opened this issue Nov 25, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@InSyncWithFoo
Copy link

Explanation

When removing a key from a dictionary, there's no need to check for its existence, as the end results will be the same anyway. Thus, dict.pop(key, None) is preferred.

Also see this issue at @astral-sh/ruff.

Example

if key in dictionary:
    del dictionary[key]

# Good
dictionary.pop(key, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants