How to get the number of times an element occurs in a list?



  • Was doing this by using IronPython, but was too slow.
    https://docs.python.org/3/library/collections.html#collections.Counter
    Would like to know if I can do it in OB.

    List = [US, CA, UK, US, DE, IN, US, UK]

    Trying to get the output in dictionary form like this (sorted by the value):
    {'US' : 3, 'UK': 2, 'CA': 1, 'DE': 1, 'IN': 1}


  • Admin

    Utility > List > Join (join the list into a single variable, maybe use a comma as separator)
    Functions > CountOccurrences

    You would need to do this for each of the expected output. Otherwise you can open an issue on OB2's repo after it goes live and I will implement a block to do this as it sounds pretty easy.

    Finally consider using js instead of ironpy as the interpreter is much faster!


Log in to reply