Add all elements of a list in Robotframework

Viewed 367

Need to sum all elements in a list in robot framework. Ex:

${count} = ['137', '130']
${Sum}=[267]-->output

Tried the Evaluate with indexing but need dynamic summation of the elements.

1 Answers

You can add all elements by using sum and map like below:

${sum} =    Evaluate    sum(map(int, ${count}))

If you need to have this sum as a list, you can to import Collections library and add the sum to a list.

*** Settings ***

Library      Collections

*** Variables ***
@{count}    137   130
@{sum_as_list}

Test sum of list

    # Initial list with numbers as strings
    Log To Console    ${count}

    # Sum as integer
    ${sum} =    Evaluate    sum(map(int, ${count}))
    Log To Console    ${sum}

    # Add sum to a list
    Append To List    ${sum_as_list}    ${sum}
    Log To Console    ${sum_as_list}
Related