Total number of unique item in a list

Hi all,

i am trying to sort the items in a list as below and create a new list. Each list have 20 items.

get the items in the list-0 at index 0 - 4,
get the items in the list-1 at index 5 - 9,
get the items in the list-2 at index 10 - 13,
get the items in the list-3 at index 14 - 19

                   OR
        
         A mixed list Nos.

thank you.

You are trying to get a list for each index number?
So all the 0’s in one list?

Try this:

numbers = IN[0]

groups = []

for number in numbers:
    # Search for a sublist that starts with the current number
    found = False
    for group in groups:
        if group[0] == number:
            group.append(number)  #Append number to correct list
            found = True
            break
    

        groups.append([number])     # Make new list if you can't find one. :)

OUT = groups

You could also do it with a dictionary.

1 Like

Seems like GroupByKey should suffice

4 Likes

Hi All,
Thanks for your replay,
Actually, I found another way. but, may I know is this the shortest way?

If you’re wanting to sort and group one list based on another list just use SortByKey and GroupByKey to sort then group the lines based on the values.

1 Like