Unstructured-IO / Unstructured-IO/unstructured-inference

[Feature]: Add __iter__() method to LayoutElements and Elements for native iteration support

Open
#436 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
209
Forks
73
PR merge metrics
No merged PRs in 30d

Description

Description

Both LayoutElements (in layoutelement.py) and Elements (in elements.py )are structured to behave like containers or array-like objects. They expose methods like .slice() and hold list/array-like data (texts, element_coords, etc.).

However, neither class implements the __iter__() method. Attempting to loop over them directly results in:

TypeError: 'LayoutElements' object is not iterable

or similarly for Elements.

Suggested Fix

Add the following to both classes:

def __iter__(self):
    return self.iter_elements()

This will enable intuitive usage like:

for el in layout_elements:
    ...

or:

for el in elements:
    ...
Motivation

This change improves the developer experience and aligns both LayoutElements and Elements with Python's collection protocol. It supports clean and readable code, especially when using list comprehensions or loops.

Benefits
  • ✅ Enables native iteration with for el in elements:
  • ✅ Enhances clarity and developer-friendliness
  • 🚫 No breaking changes (backward-compatible)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading LayoutElements in layoutelement.py and Elements in elements.py, focusing on their existing iter_elements() methods and array-like data. Add native iteration for both classes, then verify that direct loops over LayoutElements and Elements behave as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.