Skip to content

Support optional start index with FOR ... IN ENUMERATE loops #3781

Closed

Description

@pekkaklarck

The FOR ... IN ENUMERATE loop allows looping over a list of items so that you automatically get the index of the item as well:

FOR    ${index}    ${item}    IN ENUMERATE    @{items}
    Log    Item at index ${index} is '${item}'.
END

Indices start from zero, but sometimes other indexing is needed. It's possible to increment the index in the loop but that requires either using Evaluate (an extra step) or inline Python evaluation (complicated):

FOR    ${index}    ${item}    IN ENUMERATE    @{items}
    ${index} =    Evaluate    ${index} + 1
    Log    Item ${index} is '${item}'.
END
FOR    ${index}    ${item}    IN ENUMERATE    @{items}
    Log    Item ${{$index + 1}} is '${item}'.
END

The FOR ... IN ENUMERATE loop is inspired by Python's for index, item in enumerate(items) syntax and with it you can use a custom index like enumerate(items, start=1). The same approach works also well in our case:

FOR    ${index}    ${item}    IN ENUMERATE    @{items}    start=1
    Log    Item ${index} is '${item}'.
END

Activity

  1. added this to the v4.0 milestone on Nov 26, 2020
  2. added a commit that references this issue on Dec 27, 2020
    05d1463
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions