Skip to content

[SPARK-56430][PYTHON] Remove unnecessary .keys() in dict iterations#55286

Closed
zhengruifeng wants to merge 2 commits into
apache:masterfrom
zhengruifeng:py-clean-keys
Closed

[SPARK-56430][PYTHON] Remove unnecessary .keys() in dict iterations#55286
zhengruifeng wants to merge 2 commits into
apache:masterfrom
zhengruifeng:py-clean-keys

Conversation

@zhengruifeng
Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Remove unnecessary .keys() calls when iterating over dictionaries or checking dictionary length. Iterating over dict.keys() is redundant
since iterating the dict directly yields the same keys. Similarly, len(dict.keys()) can be simplified to len(dict).

Changes across 7 files:

  • pyspark/testing/utils.pylen(d.keys())len(d), for k in d.keys()for k in d
  • pyspark/memory_profiler_ext.pyfor line in measures.keys()for line in measures
  • pyspark/pipelines/cli.pyfor key in spec_data.keys()for key in spec_data
  • pyspark/pandas/frame.py — 2 locations
  • pyspark/pandas/config.py — 3 locations in DictWrapper
  • pyspark/sql/types.py — 2 locations in array typecode mapping loops
  • pyspark/pandas/namespace.py — 1 location in to_datetime

Why are the changes needed?

Code cleanup. Calling .keys() is unnecessary in these contexts since Python dicts are directly iterable over their keys, and len(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)

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
Copy link
Copy Markdown
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

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

+1, LGTM. Thank you, @zhengruifeng .

@zhengruifeng zhengruifeng deleted the py-clean-keys branch April 12, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants