Reorder a list of values by their keys according to another list of keys

Hey everyone, bear with me, I’ll try to explain what I want to achieve as best as possible but I’ve been staring myself blind on the issue so I can’t wrap my head around it anymore…

So here it goes:

List 1 is a list of strings. This is kind of the ‘mother’ list, and is the one I need to continue with… (21 instances)

List 2 is a list of values, this list is longer than list 1 and in a different order. (80 instances)

List 3 is the keys of list 2. (80 instances)

List 1 contains 16 instances of list 3.

I want a new list containing 21 instances with the 16 values from list 2 in the same order as list 1 and the other remaining 5 values as null-values.

Any ideas?

Here is a way to sort a list by an arbitrary order and filter non matching values:

Sort by arbitrary order.dyn (28.3 KB)

I don’t know if I fully understand but I’ll take a deeper look at it.
But how and where would you incorporate the null-values?

Reorder list.dyn (28.7 KB)

I made a script with example values that match mine…

Is this what you want?

1 Like

Almost,

for string 1 from list 1 = value 1 > correct!
Next should be a null value, since there is no corresponding value to string 2…

Oh sorry, i read it as…and then the following values of nulls.
so this is what you want then?

Yes exactly!

Cool.

Sorry, this code is not very robust so it might not stack up against what your real job is, but my youngling is sick so i need to tend to her.

Have a great night and hopefully someone with more skills here can help you.

master_list = IN[0]
compared_list = IN[2]
value_list = IN[1]

# Iterate over the master_list
result_list = []
for item in master_list:
    if item in compared_list:
        index = compared_list.index(item)
        if index < len(value_list):
            result_list.append(value_list[index])
        else:
            result_list.append("Null")
    else:
        result_list.append("Null")

OUT = result_list
1 Like

For now it’s holding up, so thank you very much!
Right now I’m testing my script with simple and single inputs, I’ll let you know if anything changes when I add more inputs/complexity…

@pyXam I’ve added another list level and it doesn’t work anymore.
I don’t know enough of Python to edit your code, so if you ever find some time to look at it again, let me know!
Reorder list extra level.dyn (38.1 KB)

Hi Laura,

Can you try this please.
Again, it is not a very robust code.

(But why make something super duper tripple good unless you need to… )
Probably the wrong mindset, but most of my code is too super specific to a need on a project so i don’t often find myself regurgitating something like this.

Good luck!

master_list = IN[0]
compared_list = IN[2]
value_list = IN[1]

result_list = []

# Iterate over each sublist in the master_list
for sublist in master_list:
    sublist_result = []  # Store the results for each sublist
    for item in sublist:
        sublist_index = master_list.index(sublist)  # Index of the current sublist
        if sublist_index < len(compared_list):
            sublist_compared = compared_list[sublist_index]
            sublist_value = value_list[sublist_index]
            if item in sublist_compared:
                index = sublist_compared.index(item)
                if index < len(sublist_value):
                    sublist_result.append(sublist_value[index])
                else:
                    sublist_result.append("Null")
            else:
                sublist_result.append("Null")
        else:
            sublist_result.append("Null")
    result_list.append(sublist_result)

OUT = result_list