Python Dictionaries to Create New List

Hi there,

Im trying to create a new list matching my subassembly parameters with a personalized dictionary.

image

I try to find an answer in the forum and things like that:

but im still not able to do it (sorry I delete my test, I could repeat if necessary so the problem could be check). This is the dictionary:

image

I always have this error: Unhashable XXX. I think I read there was problems with dictionaries in Dynamo, so Im not really sure how could i solve this problem.

Thank in advance

Can you show us what you’ve tried? It’s hard to tell you what the problem is if we can see what you’ve done.

Hi,

I have this Input:

This Script:

Where say dataEnteringNode is actually IN only

And This error:

image

PS: I have just basic knowledge of Phyton, I made some scripts but with really explicit guides.

Thanks in advance

Change how you are asking for the items out, just supply a single value, not the (n,n).

Hi @SeanP ,

I try like this but still the same problem… Actually my INPUT is a list of lists, dont know if that could affect…

Your inputs don’t appear to be a dictionary either. The first two sublists look the same. The easiest thing to do would be to supply the keys and the values separately or as pairs.

Yes, if you have list inputs, then you’d need to iterate those as well while looking to get values.

1 Like

hello
try this

OUT = [[dic.get(n) for n in sublist] for sublist in IN[0]]

6 Likes

Thanks a lot @c.poupin , that actually works but, can you explain a bit the logic behind this? As I said Im starting with Phyton and some things looks magic for me

Hello @manumolina96
it’s a Nested List Comprehensions to keep the structure of the entry list
some explanation here

1 Like

Here is an example of what the expanded form would look like:

Lists = IN[0]

output = []

for sublist in Lists:
    newItems =[]
    for item in sublist:
        newItems.append(dic.get(item))
    output.append(newItems)

OUT = output
2 Likes