# PYTHON collecting schedules NOT on sheets for pyREvit

**URL:** <https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586>\
**Category:** Developers\
**Tags:** revit, python, dynamo\
**Created:** [September 29, 2022, 1:10am UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586 "2022-09-29T01:10:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 29, 2022, 1:10am UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/1 "2022-09-29T01:10:19Z")

</div>

So working on a pythonic way to get schedules NOT on sheets. there is [Collecting Legends and Schedules not on Sheets](https://forum.dynamobim.com/t/collecting-legends-and-schedules-not-on-sheets/20538) but that uses nodes, and I am trying to work backwards to a tool I can use in PyREvit.

I have this:  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/c/5cf88e087d6c1768f57ccff72610d860d06c2447.png)

which proves this works:

```auto
b=[1,3,5,6,7,8]
a=[1,2,3]

c=[element for i, element in enumerate(b) if element not in a]
OUT=c

```

But when I do this:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/c/ac201c9c140cd3bcdd2043b190f302bc1184989a.png)

The VSH\_OFF Isn’t returning the difference  
(The 2nd #VSH\_OFF line is with out the ‘i’ - is this an enumerator for the list position as it steps?)

```auto
##Insipred from 
##https://forum.dynamobim.com/t/collecting-legends-and-schedules-not-on-sheets/20538/2
##Apsis0215 R Allen 2022-09-28 CPy3
import clr

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument ##Current Document

#clr.AddReference("RevitNodes")
import Revit
#clr.ImportExtensions(Revit.Elements)

#clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector,ScheduleSheetInstance,ViewSchedule

PlacedSchedules = FilteredElementCollector(doc).OfClass(ScheduleSheetInstance).WhereElementIsNotElementType().ToElements()
PlacedSchedulesID=[VID.Id.IntegerValue for VID in PlacedSchedules]

AllSchedules = FilteredElementCollector(doc).OfClass(ViewSchedule).WhereElementIsNotElementType().ToElements()
AllSchedulesID =[VID.Id.IntegerValue for VID in AllSchedules]

##https://stackoverflow.com/questions/33577790/exclude-items-from-list-of-lists-python
##VSH_OFF should be all elements NOT matching 
VSH_OFF=[element for i, element in enumerate(AllSchedulesID) if element not in PlacedSchedulesID]
#VSH_OFF=[element for element in AllSchedulesID if element not in PlacedSchedulesID]

#vsh_ON=[]

HostSheets = [doc.GetElement(VID.get_OwnerViewId()) for VID in PlacedSchedules] ##Get host sheet for each schedsheet

#for vsh in AllSchedulesID:
# if vsh in PlacedSchedulesIDs:
# vsh_ON.append(vsh)
# else:
# VSH_OFF.append(vsh)

OUT=PlacedSchedulesID,AllSchedulesID,VSH_OFF ##PlacedSchedulesIDs,AllSchedulesID 

```

Tried a couple of ways now - but no dice- any suggestions?

and is CPy3 going to work in PyRevit?(Anyone know yet?)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [September 29, 2022, 5:13am UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/2 "2022-09-29T05:13:26Z")

</div>

Pyrevit supports CPython3 if you add this to the top of your py file:

```auto
#!python3

```

Also try getting the schedule’s type Id instead. This should work I think…

```auto
# Made by Gavin Crump
# Free for use
# BIM Guru, www.bimguru.com.au

# Boilerplate text
import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 

clr.AddReference("RevitAPI")
import Autodesk 
from Autodesk.Revit.DB import *

# Current doc/app/ui
doc = DocumentManager.Instance.CurrentDBDocument

# Define list/unwrap list functions
schedTypes = FilteredElementCollector(doc).OfClass(ViewSchedule).ToElements()

schedInsts = FilteredElementCollector(doc).OfClass(ScheduleSheetInstance).ToElements()

instIds = [s.ScheduleId for s in schedInsts]

unused = []

for s in schedTypes:
	if s.Id not in instIds:
		unused.append(s)

# Preparing output to Dynamo
OUT = unused

```

---

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 30, 2022, 1:31am UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/3 "2022-09-30T01:31:42Z")

</div>

Soo cool ! Thanks!

---

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 30, 2022, 5:54pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/4 "2022-09-30T17:54:40Z")

</div>

@GavinNicholls that was not the solution to the original question whis is:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/7/377702b7d0430c07923d939953a91fdbc8d003ba.png)

The Proof of concept works but the List in list doesn’t purge like it should.

I can write a DEF to do it - would would prefer the pythonic way of managing the list!

Is this a #pythonBug or is my syntax off somewhere I am not seeing?

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [September 30, 2022, 6:58pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/5 "2022-09-30T18:58:52Z")

</div>

```auto
##Insipred from 
##https://forum.dynamobim.com/t/collecting-legends-and-schedules-not-on-sheets/20538/2
##Apsis0215 R Allen 2022-09-28 CPy3
import clr

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument ##Current Document

#clr.AddReference("RevitNodes")
import Revit
#clr.ImportExtensions(Revit.Elements)

#clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector,ScheduleSheetInstance,ViewSchedule

PlacedSchedules = FilteredElementCollector(doc).OfClass(ScheduleSheetInstance).WhereElementIsNotElementType().ToElements()
PlacedSchedulesID=[VID.ScheduleId for VID in PlacedSchedules]

AllSchedules = FilteredElementCollector(doc).OfClass(ViewSchedule).WhereElementIsNotElementType().ToElements()
AllSchedulesID =[VID.Id for VID in AllSchedules]

##https://stackoverflow.com/questions/33577790/exclude-items-from-list-of-lists-python
##VSH_OFF should be all elements NOT matching 
VSH_OFF=[sched for sched in AllSchedulesID if sched not in PlacedSchedulesID]
#VSH_OFF=[element for element in AllSchedulesID if element not in PlacedSchedulesID]

#vsh_ON=[]

HostSheets = [doc.GetElement(VID.get_OwnerViewId()) for VID in PlacedSchedules] ##Get host sheet for each schedsheet

#for vsh in AllSchedulesID:
# if vsh in PlacedSchedulesIDs:
# vsh_ON.append(vsh)
# else:
# VSH_OFF.append(vsh)

OUT=PlacedSchedulesID,AllSchedulesID,VSH_OFF ##PlacedSchedulesIDs,AllSchedulesID

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/a/facf503fa5662599544d502943952a9a4d96b730.png)

---

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 30, 2022, 7:05pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/6 "2022-09-30T19:05:42Z")

</div>

That worked! Still trying to find where I am “off” : ) Working through it now : )

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [September 30, 2022, 7:22pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/7 "2022-09-30T19:22:33Z")

</div>

Take a look at the changed I made to this part as well. I don’t think you were getting the correct ScheduleId from the instances.

> [@Ron\_Allen](#):
>
> ```auto
> PlacedSchedules = FilteredElementCollector(doc).OfClass(ScheduleSheetInstance).WhereElementIsNotElementType().ToElements()
> PlacedSchedulesID=[VID.Id.IntegerValue for VID in PlacedSchedules]
> 
> AllSchedules = FilteredElementCollector(doc).OfClass(ViewSchedule).WhereElementIsNotElementType().ToElements()
> AllSchedulesID =[VID.Id.IntegerValue for VID in AllSchedules]
> 
> ```

If you think about this as an expanded loop, you will see where it broke down, which is basically what @GavinNicholls showed in his example now that I actually look at it.

> [@GavinNicholls](#):
>
> ```auto
> for s in schedTypes:
> if s.Id not in instIds:
> unused.append(s)
> 
> ```

This will wrap up to:

VSH\_OFF = [s for s in schedTypes if s.Id not in instIds]

> [@Ron\_Allen](#):
>
> `VSH_OFF=[element for i, element in enumerate(AllSchedulesID) if element not in PlacedSchedulesID]`

---

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 30, 2022, 8:00pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/8 "2022-09-30T20:00:03Z")

</div>

@SeanP Freaking awesome catch!

Frustrating for us- now I see now the correct:

1. [VID.Id.IntegerValue for VID in PlacedSchedules]

not the same as the _expected_ Broken:

1. [VID.Id.IntegerValue for VID in PlacedSchedules]  
( **NOTE:** x.Integer value tested and moot either way)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/f/4fbfa2f8f9f491dfebd647d154f15c29fa66bef5.png)

---

<div class="post-metadata">

**Author:** ![Ron\_Allen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ron_allen/32/124849_2.png) [@Ron\_Allen](https://forum.dynamobim.com/u/Ron_Allen)\
**Post date:** [September 30, 2022, 8:56pm UTC](https://forum.dynamobim.com/t/python-collecting-schedules-not-on-sheets-for-pyrevit/81586/9 "2022-09-30T20:56:51Z")

</div>

Pretty version cleaned up with meaningful names : )

```auto
#!python3 ##Code pre-directive untested!
##Apsis0215 R Allen 2022-09-28 CPy3
import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument ##Current Document
import Revit
from Autodesk.Revit.DB import FilteredElementCollector,ScheduleSheetInstance,ViewSchedule

PlacedSchedules = FilteredElementCollector(doc).OfClass(ScheduleSheetInstance).WhereElementIsNotElementType().ToElements()
PlacedSchedulesName = [PSS.Name for PSS in PlacedSchedules]
PlacedSchedulesID=[VID.ScheduleId for VID in PlacedSchedules]

AllSchedules = FilteredElementCollector(doc).OfClass(ViewSchedule).WhereElementIsNotElementType().ToElements()
AllSchedulesID =[VID.Id for VID in AllSchedules]

##https://stackoverflow.com/questions/33577790/exclude-items-from-list-of-lists-python
SchedNotPlaced=[element for element in AllSchedulesID if element not in PlacedSchedulesID]
SchedNotPlaced=[doc.GetElement(VSHID) for VSHID in SchedNotPlaced]
SchedNotPlacedNames=[NPS.Name for NPS in SchedNotPlaced]

HostSheet = [doc.GetElement(VID.get_OwnerViewId()) for VID in PlacedSchedules] ##Get host sheet for each schedsheet
HostSheetsName=[HS.Name for HS in HostSheet] ##REturn host sheet name for 

OUT=PlacedSchedulesName,HostSheetsName,SchedNotPlacedNames ##PlacedSchedulesIDs,AllSchedulesID

```
