Having trouble getting my Python script to cooperate with my list

I’ve found the indices of two values in my list and now I am just trying to use the List.Flatten(list, Indices) command to remove those indices from the main list. It is telling me that I have an error "Expected Array[Int], got List[Object]. And I’m stumped.

What can I do?

   import clr
clr.AddReference('RevitAPI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
clr.AddReference('RevitAPIUI')
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *

#import sys	#clr.ImportExtensions(Revit.Elements)	#from Revit.Elements import *	#clr.ImportExtensions(Revit.GeometryConversion)	#from Revit.GeometryConversion import *	#clr.ImportExtensions(Revit.GeometryReferences)	#from Revit.GeometryReferences import *	#import System	#from System.Collections.Generic import *	#from System import *

####### Trying to remove the specified values from IN[0] list
######Step 1: Get all indices of values present in current list
#####Step 2: Remove Items at said Indices

list=IN[0]					#List of parameter values to 
#ShortList=IN[1]	#list of string values
str1=("Type Id : 307623")	#value to remove from list
str2=("Type Id : 308609")	#value to remove from list

cnt=len(list)
inx=0
indices=[]

num1 = List.AllIndicesOf(list, str1);
num2 = List.AllIndicesOf(list, str2);
lst = [num1, num2];
indices = List.Flatten(lst, -1);

output = List.RemoveItemAtIndex(list, indices);

#Assign your output to the OUT variable

OUT = output

I am not sure if this is what you want.
lists=IN[0]

str1=("Type Id : 307623")	
str2=("Type Id : 308609")	

strs = [str1, str2]

lists = list(filter(lambda a: a not in strs, x))

OUT = output
4 Likes

Can you show the inputs to the Python node? Also is there a specific reason why you are doing this in Python instead of just a code block? All of the functions you are using are DS nodes.

1 Like

you could try something like:
list.pop(list.index(“Type Id : 307623”))

Also, this is just a shot in the dark, but I remember having issues in the past when using the variable name ‘list’ to call a list when also using the List.(function) functions so maybe you could try changing that.

3 Likes

@mrkevinht91 It’s telling me that X is undefined, yet when I pre-define “x=0” it complains about the “lists =…” line:

TypeError: int is not iterable

Also, I’ve changed the “list” value to the name of “bob” with respect to what @martin.scholl has mentioned. I’ll leave it as such for the time being, though I may try changing it back once I have everything in tip top shape.

import clr
clr.AddReference('RevitAPI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
clr.AddReference('RevitAPIUI')
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *

#import sys	#clr.ImportExtensions(Revit.Elements)	#from Revit.Elements import *	#clr.ImportExtensions(Revit.GeometryConversion)	#from Revit.GeometryConversion import *	#clr.ImportExtensions(Revit.GeometryReferences)	#from Revit.GeometryReferences import *	#import System	#from System.Collections.Generic import *	#from System import *

####### Trying to remove the specified values from IN[0] list
######Step 1: Get all indices of values present in current list
#####Step 2: Remove Items at said Indices

bob=IN[0]					#List of parameter values to 
#ShortList=IN[1]	#list of string values
str1=("Type Id : 307623")	#value to remove from list
str2=("Type Id : 308609")	#value to remove from list
strs = [str1, str2]
cnt=len(bob)
inx=0
indices=[]
Output = 0
x = 0

lists = bob(filter(lambda a: a not in strs, x))

num1 = List.AllIndicesOf(bob, str1);
num2 = List.AllIndicesOf(bob, str2);
lst = [num1, num2];
indices = List.Flatten(lst, -1);
Uni = List.UniqueItems(bob)
count = List.Count(indices)

#output = List.RemoveItemAtIndex(list, indices);
#NewID = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z];
#if NewID==(0):
#	List.RemoveItem
#List.RemoveIfNot(NewID, (>=0));

OUT = output

@kennyb6 I want to use Python almost exclusively so that I can eventually create a button in the Revit UI to bypass using the Dynamo player all together. Essentially I am using Dynamo as a testing ground for my Python script. Also, I have a snapshot below that shows the input list of strings values for the Python node to see.

If your end goal is to make a python button in revit, then you will need to remove all references to dynamo and not utilize any dynamo nodes within the python script.

Therefore you may need to look at python list management as a trade of!

1 Like

Very good point, thank you for pointing this out! I’ll do some googling and read up.

Sorry about the ‘X’. I try with variable X in my vscode then post it directly to here without paying a good attention. You should replace ‘X’ by ‘list’ and it will works fine.

There’s a much simpler way to do this. Something like this:

inputList = IN[0]
shortList = IN[1]

output = []

for i in inputList:
	if i not in shortList:
		output.append(i)
	
OUT = output
3 Likes

So I apologize all who have been working on helping me. I had forgotten that I posted this. I’ve gotten my script to work and below is what my script currently looks like. Thank you all for all your help! I really am absurdly impressed and grateful for the help from this website/forum! This really is an invaluable asset!

import clr
clr.AddReference('RevitAPI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
clr.AddReference('RevitAPIUI')
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import collections
from sys import *
import string
#import numpy as np




# Trying to remove the specified values from IN[0] list
##Step 1: Get all indices of values present in current list
###Step 2: Remove Items at said Indices


#Variables

lost = IN[0]
lst = IN[0]			#list of all Parameter (Type Id) values
ElSel = IN[1]		#list of all selected elements
NULL = IN[2]
output = set()
elementlist = []
Weld1 = "307623";	#SS Weld Type Id to be omitted
Weld2 = "308609";	#CS Weld Type Id to be omitted
cnt=len(lost)
A1 = map(chr, range(65, 91))	#Creating an Alphabet for the Renumber Values
items = lost
Uniq = []






#######

#filtering out known weld Type Id values and replacing weld locations with `null`
elementlist = list()
for item in lost:
	if item == Weld1:
		item = None
	elif item == Weld2:
		item = None
	elementlist.append(item)
count = len(elementlist)

Uniq = List.UniqueItems(elementlist)
#Removing Null Value from Unique Value list
Uniq.Remove(None)

To be honest I’m not sure if I have used anypne’s suggestion(s), let me know if I have and I’ll gladly credit you’re reply with a solution mark.

Though I will say if anyone has a quick tip as to the process of importing numpy. I don’t think I need it anymore for this script but it may come in handy in the future. And again thank you all whom have helped!