[SPARK-56430][PYTHON] Remove unnecessary .keys() in dict iterations#55286
Closed
zhengruifeng wants to merge 2 commits into
Closed
[SPARK-56430][PYTHON] Remove unnecessary .keys() in dict iterations#55286zhengruifeng wants to merge 2 commits into
zhengruifeng wants to merge 2 commits into
Conversation
Iterating over `dict.keys()` is redundant when iterating the dict directly suffices. Similarly, `len(dict.keys())` can be simplified to `len(dict)`. Co-authored-by: Isaac
Co-authored-by: Isaac
dongjoon-hyun
approved these changes
Apr 10, 2026
Member
dongjoon-hyun
left a comment
There was a problem hiding this comment.
+1, LGTM. Thank you, @zhengruifeng .
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Remove unnecessary
.keys()calls when iterating over dictionaries or checking dictionary length. Iterating overdict.keys()is redundantsince iterating the dict directly yields the same keys. Similarly,
len(dict.keys())can be simplified tolen(dict).Changes across 7 files:
pyspark/testing/utils.py—len(d.keys())→len(d),for k in d.keys()→for k in dpyspark/memory_profiler_ext.py—for line in measures.keys()→for line in measurespyspark/pipelines/cli.py—for key in spec_data.keys()→for key in spec_datapyspark/pandas/frame.py— 2 locationspyspark/pandas/config.py— 3 locations inDictWrapperpyspark/sql/types.py— 2 locations in array typecode mapping loopspyspark/pandas/namespace.py— 1 location into_datetimeWhy are the changes needed?
Code cleanup. Calling
.keys()is unnecessary in these contexts since Python dicts are directly iterable over their keys, andlen(dict)already returns the number of keys.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
Existing tests (no behavioral change).
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (claude-opus-4-6)