# Adding iteration functionality to ItemizedResource

**URL:** <https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42>\
**Category:** dev discussion\
**Created:** [October 11, 2024, 1:54am UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42 "2024-10-11T01:54:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marielle](https://discuss.pylabrobot.org/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@marielle](https://discuss.pylabrobot.org/u/marielle)\
**Post date:** [October 11, 2024, 1:54am UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42/1 "2024-10-11T01:54:17Z")

</div>

Hi everyone,

I’m new to the forum and have a suggestion I’d like to discuss. I’ve been working with ItemizedResources quite a bit lately, and I believe adding an `iter()` and associated method would improve their usability and efficiency.

In my current projects, I often need to iterate through the items in an ItemizedResource. Right now, this requires manual logic loops, which can be cumbersome and less efficient than a built-in iterator.

Before I dive deeper into a formal proposal, I wanted to get the community’s thoughts on this idea. Specifically, I’m interested in:

- **Potential drawbacks:** Are there any reasons why adding `iter()` might be problematic or undesirable?
- **Desired features:** If we were to implement this, what specific features or functionalities should the iterator include? (e.g., filtering, sorting, etc.)

I’m looking forward to hearing your feedback and exploring this possibility further!

---

<div class="post-metadata">

**Author:** ![rickwierenga](https://discuss.pylabrobot.org/user_avatar/discuss.pylabrobot.org/rickwierenga/32/963_2.png) [@rickwierenga](https://discuss.pylabrobot.org/u/rickwierenga)\
**Post date:** [October 11, 2024, 10:12pm UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42/2 "2024-10-11T22:12:00Z")

</div>

there’s already the `ItemizedResource.traverse` method that could fit your needs. If you’re missing anything, please make a PR 🙂

---

<div class="post-metadata">

**Author:** ![CamilloMoschner](https://discuss.pylabrobot.org/user_avatar/discuss.pylabrobot.org/camillomoschner/32/7_2.png) [@CamilloMoschner](https://discuss.pylabrobot.org/u/CamilloMoschner)\
**Post date:** [October 15, 2024, 3:41pm UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42/3 "2024-10-15T15:41:25Z")

</div>

> [@marielle](#):
>
> **Potential drawbacks:** Are there any reasons why adding `iter()` might be problematic or undesirable?

I find the use of iterators for programmatic sample management very powerful.  
But regarding drawbacks to using an iterator directly associated with `ItemizedResource`:  
It only provides a solution for every `ItemizedResource` instance.

It’s unlikely that you’ll only use e.g. a single `Plate` for an assay.  
If you have 3 `Plate`s then, in this scenario, you have to manage 3 separate iterators… that’s not efficient.

You could circumvent this issue by concatenating/chaining the iterators, e.g.:

```python
import itertools

# Example iterators
iter1 = iter([1, 2, 3])
iter2 = iter([4, 5, 6])
iter3 = iter([7, 8, 9])

# Concatenate the iterators
concatenated_iter = itertools.chain(iter1, iter2, iter3)

# Iterate over the concatenated iterator
for value in concatenated_iter:
    print(value)

```

But what if you want to - as you eluted to - sort the wells you want to iterate over first?  
(e.g. process all plates’ columns first before moving to the next column… very useful)

I prefer sorting the children of plates first, then creating an iterator over the sorted list.  
This is quite specific to the automated protocols you’re running, and therefore is likely to be defined specifically 🙂

---

<div class="post-metadata">

**Author:** ![rickwierenga](https://discuss.pylabrobot.org/user_avatar/discuss.pylabrobot.org/rickwierenga/32/963_2.png) [@rickwierenga](https://discuss.pylabrobot.org/u/rickwierenga)\
**Post date:** [October 15, 2024, 4:54pm UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42/4 "2024-10-15T16:54:48Z")

</div>

for traversing tip spots in multiple tip racks i added this [add tip spot generators by rickwierenga · Pull Request #256 · PyLabRobot/pylabrobot · GitHub](https://github.com/PyLabRobot/pylabrobot/pull/256), maybe we can have something similar for Wells, or even use the same code

---

<div class="post-metadata">

**Author:** ![marielle](https://discuss.pylabrobot.org/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@marielle](https://discuss.pylabrobot.org/u/marielle)\
**Post date:** [October 15, 2024, 6:13pm UTC](https://discuss.pylabrobot.org/t/adding-iteration-functionality-to-itemizedresource/42/5 "2024-10-15T18:13:19Z")

</div>

Thanks for the feedback! The tip spot generators largely resolves the issue that was making my code a bit more complex than it felt like it needed to be, I think it might be good to generalize the use though, particularly for mapping from a TipRack to sets of plates, that could be very powerful.
