What I hardly understand is that the list of the category can not be selected with a certain index number but it works when I feed the key(such as “Walls”) like a dictionary.
Can anybody advise what I am missing here? The list ‘cat’ in my code is a list, not a dictionary, isn’t it?
My code lists below.
import clr
import sys
import System
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
import System
doc = DocumentManager.Instance.CurrentDBDocument
out = []
out_1 = []
cat_selectionLst = IN[0]
cat = doc.Settings.Categories
catName = [x.Name for x in cat]
cat_db_index = [catName.index(i) for i in cat_selectionLst]
catLst = [cat[j] for j in cat_selectionLst]
#catLst_1 = [cat[k] for k in cat_db_index]
OUT = catLst
So it’s a bit confusing because the Python uses ‘list comprehension’ which is a different way of writing an if statement…
for thing in list:
do something to thing
becomes:
[do something to thing for thing in list]
The ‘key’ is the category name which is being pulled when you input it x.Name, so that’s why it is returned a bit like a dictionary, but not using a python dictionary to do so… If that makes sense?
Thank you for your advice!
Unfortunately, it seems that using list comprehension doesn’t affect working like a dictionary as it shows the same result when I use the typical For in statement. But when I make it another list from category list pulled at first(marked with the blue arrow) I can select items with indices as I initially intended. Presumably, just calling categories may not be considered as a list, I guess.