Can't create dictionary with integer keys in python script

I’m working with Revit 2021. Within a python script, I wanted to create a dictionary with integer values as keys but got an error

This is very strange because python itself allows this. Also, I did it before in Dynamo, although not Revit 2021 - has anything changed or am I missing something?

what are you trying to do? It seems you want the index of IN[0]?
what is you IN[0]?

if you are using a integer list in IN[0] … that won’t work since lns[i] will be out of range …

btw: “range(len(lns))” is the same as using just “lns”

mylist2 = [2, 4, 56, 477]
mydict = {}
for index, val in enumerate(mylist2):
    mydict[index] = val

print mydict
# {0: 2, 1: 4, 2: 56, 3: 477}

Thanks for the answer. I didn’t realize the ambiguity, IN[0] is itself a list of line objects.

There is no error in my syntax actually. I have just found out that the error only occurs when I try to pass the dictionary out of the python script and into Dynamo. As long as the dictionary is only used within the script, it works fine. When I pass it out, it looks like it automatically tries to convert integer keys to strings and fails.

So is it Dynamo itself that cannot handle integer keys? Can anyone clarify?

Hi @vancikv
Yes, in Dynamo, dictionary keys are only strings

Thanks. Good thing I only need the dictionary inside the script. I only passed it out to check the value.