# Python Def not looping through all items

**URL:** <https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356>\
**Category:** Developers\
**Tags:** revit, python, dynamo\
**Created:** [April 27, 2021, 6:14pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356 "2021-04-27T18:14:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick\_podeyn](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick_podeyn/32/86368_2.png) [@patrick\_podeyn](https://forum.dynamobim.com/u/patrick_podeyn)\
**Post date:** [April 27, 2021, 6:14pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/1 "2021-04-27T18:14:51Z")

</div>

Hello all!

I previously found a solution to extract the element type, and name from an import instance. I’m now trying to use that code as a definition, and it seems like it only loops through one of the items in the list. I tried doing another for loop for the cad import instances but I get an _iteration over non-sequence of type_ error. I’m not 100% sure but it seems like I’m looping wrong. Here is a screenshot of my python and the information going into IN[1] is a true bool.

**Thanks in advance!**

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

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

# Boilerplate text
import clr

import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')

import System
from System import Array
from System.Collections.Generic import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

# Current doc/app/ui
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

def worksetCreator(element, eXworkset):
	for i in element:
		elemType = (doc.GetElement(i.GetTypeId()))
		elemName = (Element.Name. __get__ (elemType))
		elemRName = elemName[:-4]
		if elemRName not in eXworkset:
			return i, elemType, elemRName

# Define list/unwrap list functions
# Preparing input from dynamo to revit
cad = FilteredElementCollector(doc).OfClass(ImportInstance).ToElements()
unWcad = UnwrapElement(cad)
pntCloudInt = UnwrapElement(FilteredElementCollector(doc).OfClass(PointCloudInstance).ToElements())
Navis = UnwrapElement(FilteredElementCollector(doc).OfClass(DirectShape).ToElements())
eXworkset = IN[0]
run = IN[1]

# Do some action in a Transaction
#TransactionManager.Instance.EnsureInTransaction(doc)
# your actions
#TransactionManager.Instance.TransactionTaskDone()
result = []
if run == True:
#	try:
	cadEle = (worksetCreator(unWcad, eXworkset))
	result.append(cadEle)
#	except:
# pntCloudEle = (worksetCreator(pntCloudInt, eXworkset))
# result.append(pntCloudEle)
#	else:
# navisEle = (worksetCreator(Navis, eXworkset))
# result.append(navisEle)
#	finally:
# result.append("WL")
else:
	result.append("Workset Already Created")
# Output and Changing element to Dynamo for export
# <element>.ToDSType(True), #Not created in script, mark as Revit-owned
# <element>.ToDSType(False) #Created in script, mark as non-Revit-owned
OUT = result
```

---

<div class="post-metadata">

**Author:** ![blsalvio](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@blsalvio](https://forum.dynamobim.com/u/blsalvio)\
**Post date:** [April 27, 2021, 11:54pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/2 "2021-04-27T23:54:44Z")

</div>

hi @patrick_podeyn

It will surely not loop because when the conditions of the **`IF`** statement is **once** satisfied, the function will immediately return a value and not proceed any more. The solution to your problem is to define a list variable before the for-loop and append `i, elemType, elemName` to that variable.

like this:

```
def worksetCreator(element, eXworkset):
	output = list()

	for i in element:
		elemType = (doc.GetElement(i.GetTypeId()))
		elemName = (Element.Name. __get__ (elemType))
		elemRName = elemName[:-4]

		if elemRName not in eXworkset:
			output.append(i, elemType, elemRName)

	return output
```

---

<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:** [April 28, 2021, 1:11am UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/3 "2021-04-28T01:11:19Z")

</div>

Might want to make sure you don’t keep my name on the top of the boilerplate in the final form 😉

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 28, 2021, 8:17am UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/4 "2021-04-28T08:17:31Z")

</div>

@GavinNicholls maybe a silly question but why?  
If I find some code I always keep the originator’s name.  
It’s good for reference and it’s also making sure no one thinks I know how to fix it if it breaks. 😋

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [April 28, 2021, 8:24am UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/5 "2021-04-28T08:24:46Z")

</div>

Yeah, I happen to agree with @alien here, I would always give credit where credit is due. If the script is based on other code, I would simply state around the part I have taken that this is based up on the work by [Author] and lavish them with thumbs ups! 🙂

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [April 28, 2021, 8:46am UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/6 "2021-04-28T08:46:19Z")

</div>

I often put the URL of where it came from too. That can be useful in future.

---

<div class="post-metadata">

**Author:** ![patrick\_podeyn](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick_podeyn/32/86368_2.png) [@patrick\_podeyn](https://forum.dynamobim.com/u/patrick_podeyn)\
**Post date:** [April 28, 2021, 2:39pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/7 "2021-04-28T14:39:51Z")

</div>

I have it in there to remember who the awesome person is that created this awesome thing. Also when someone else goes in to look at the python code, they too can recognize who the awesome person is that created the awesome thing.

---

<div class="post-metadata">

**Author:** ![patrick\_podeyn](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick_podeyn/32/86368_2.png) [@patrick\_podeyn](https://forum.dynamobim.com/u/patrick_podeyn)\
**Post date:** [April 28, 2021, 2:46pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/8 "2021-04-28T14:46:49Z")

</div>

I’m getting an error that I’m appending too many arguments into the output. I just tried to create a blank list for each output and it only returned the one instance.

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

---

<div class="post-metadata">

**Author:** ![patrick\_podeyn](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick_podeyn/32/86368_2.png) [@patrick\_podeyn](https://forum.dynamobim.com/u/patrick_podeyn)\
**Post date:** [April 28, 2021, 3:01pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/9 "2021-04-28T15:01:14Z")

</div>

@blsalvio I think I found the solution, I need to use output.extend(a,b,c) instead of append. Now I need to figure out how to break the list into sublists.

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

---

<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:** [April 28, 2021, 4:00pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/10 "2021-04-28T16:00:17Z")

</div>

FYI, no need to unwrap elements that come from FECs since they are already native Revit elements. You only need to Unwrap Dynamo elements if they are supplied from an Input (Green I’d items).

---

<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:** [April 28, 2021, 4:01pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/11 "2021-04-28T16:01:19Z")

</div>

In this case I think it wasn’t my code, but they’re likely using my boilerplate template. I’m humbled in either case (and always admire Patrick’s work, so no intended offense), but I don’t recognise any of the code in the screenshots. Happy for him to take credit over whatever solution he reaches 🙂

---

<div class="post-metadata">

**Author:** ![patrick\_podeyn](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick_podeyn/32/86368_2.png) [@patrick\_podeyn](https://forum.dynamobim.com/u/patrick_podeyn)\
**Post date:** [April 28, 2021, 4:06pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/12 "2021-04-28T16:06:00Z")

</div>

Yes, I am using your supplied boilerplate then adding and modifying the code. I’ll probably change it to say “_BoilerPlate awesomeness supplied by Gavin Crump_” 🙂

---

<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:** [April 28, 2021, 4:07pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/13 "2021-04-28T16:07:17Z")

</div>

Haha glad it is of use. Be aware I’ve found an issue in the Unwrap list function (def uwlist etc.) recently. The returned element is specified as ‘Input’, but should actually be ‘result’. Whoops!

Just uploaded the updated template to github.

---

<div class="post-metadata">

**Author:** ![blsalvio](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@blsalvio](https://forum.dynamobim.com/u/blsalvio)\
**Post date:** [April 28, 2021, 10:48pm UTC](https://forum.dynamobim.com/t/python-def-not-looping-through-all-items/63356/14 "2021-04-28T22:48:36Z")

</div>

hi @patrick_podeyn ,

my bad, it shoud be:

`output.append([i, elemType, elemRName])`
