# Python Nodes Basics

**URL:** <https://forum.dynamobim.com/t/python-nodes-basics/15872>\
**Category:** Share\
**Tags:** learning\
**Created:** [October 24, 2017, 2:41pm UTC](https://forum.dynamobim.com/t/python-nodes-basics/15872 "2017-10-24T14:41:26Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [November 1, 2017, 10:09pm UTC](https://forum.dynamobim.com/t/python-nodes-basics/15872/12 "2017-11-01T22:09:30Z")

</div>

The following posts are related to **PYTHON LISTS** :

> [@Running Node on Several Background Opened Files](https://forum.dynamobim.com/t/running-node-on-several-background-opened-files/16369/2):
>
> @staylor You have to iterate over your list of documents using a for loop. Right now you are defining doc as a list, so essentially in line 18 you are saying param\_list = ListObject.FamilyManager.GetParameters() See below example, when I query the object type of the input without a for loop, it shows me the object is a list. If I query the object type of the input elements with the for loop, it returns the object type of the items in the list So, you need to put your code w…

> [@Select Pinned Elements](https://forum.dynamobim.com/t/select-pinned-elements/15421/5):
>
> Hi @Jordan_Billingsley - I already have a very simple Python script that accomplishes supplying a boolean output for whether or not elements are pinned are not, which I will provide below. You can use this inputting your wall elements, then using Filter.ByBoolMask with its output. To encourage you to understand how the script works & to further learn using Python with the Revit API I will provide an explanation on how it works. if you look at the API Docs that @Nick_Boyts referenced above you w…

> [@Algorithm for sublist in python](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/3):
>
> There’s probably a cleaner way of doing this but a simple use of append will work. import clr clr.AddReference('ProtoGeometry') from Autodesk.DesignScript.Geometry import \* clr.AddReference('DSCoreNodes') from DSCore import \* #The inputs to this node will be stored as a list in the IN variables. dataEnteringNode = IN items = IN[0] inlist = IN[1] newlist = [] for indices in inlist: sublist = [] for i in indices: sublist.append(items[i]) newlist.append(sublist) #Assign your output to the O…

> [@Python Error output](https://forum.dynamobim.com/t/python-error-output/15599/2):
>
> Strange. Works fine on my end. Have you tried closing the file and re-running it?

> [@Stuck with a python logic](https://forum.dynamobim.com/t/stuck-with-a-python-logic/16269/3):
>
> As an FYI, you should paste your Python code using the Preformatted Text option. Just highlight your pasted text and click \</\> in the toolbar above. Yes, but I don’t think that’s what you need here. As @awilliams said, you can wrap your if statement within your for loop. However, if you wanted to iterate through both lists simultaneously you would use: for i, j in zip(list1, list2): This would essentially get you paired values list1[0]/list2[0], list1[1]/list2[1], etc.

> [@Inserting element into list with python](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/3):
>

[https://forum.dynamobim.com/t/lacing-on-replacing-item-on-index/16462/11](https://forum.dynamobim.com/t/lacing-on-replacing-item-on-index/16462/11)  
[https://forum.dynamobim.com/t/help-with-group-by-key/11379/8](https://forum.dynamobim.com/t/help-with-group-by-key/11379/8)

> [@Setting Revision Visibility with Python - RevisionVisibility Enumeration Members](https://forum.dynamobim.com/t/setting-revision-visibility-with-python-revisionvisibility-enumeration-members/15993/8):
>
> I have noticed that this before too, and it looks like UnwrapElement() is converting the Pyton List into a Collections.Generic.List. That’s why isintance() returns false.

> [@List Merge](https://forum.dynamobim.com/t/list-merge/8918/3):
>
> @Cesar_Escalante Using Python, option 1: def MergeSort(lst1,lst2): MergedList=[] for index,i in enumerate(lst1): MergedList.append(i+lst2[index]) MergedList[index].sort() return MergedList OUT=MergeSort(IN[0],IN[1]) Option 2: MergedList=[i+IN[1][index] for index,i in enumerate(IN[0])] for i in MergedList: i.sort() OUT=MergedList

> [@How to create ElementSet from list of elements in Dynamo](https://forum.dynamobim.com/t/how-to-create-elementset-from-list-of-elements-in-dynamo/16067/2):
>
> hi @andre.abotnes [image]

> [@Automatic dimensioning of (multiple) detail lines](https://forum.dynamobim.com/t/automatic-dimensioning-of-multiple-detail-lines/14886/2):
>
> Hi JN, Have you tried setting the lacing of the custom node that you created to Longest? Otherwise you could use a for loop in Python: import clr clr.AddReference('RevitAPI') from Autodesk.Revit.DB 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 TransactionM…

> [@New list with largest value among sublists ; Python, Lists](https://forum.dynamobim.com/t/new-list-with-largest-value-among-sublists-python-lists/7228/8):
>
> the function works with nulls as well : we take each element among sublists and then find largest i’m not sure I got this part

> [@Use the Flatten node in python script](https://forum.dynamobim.com/t/use-the-flatten-node-in-python-script/10363/11):
>
> @ Hannes - use List Comprehensions - they are much faster than loops: import clr clr.AddReference(‘ProtoGeometry’) from Autodesk.DesignScript.Geometry import \* dataEnteringNode = IN[0] OUT = [i for sublist in IN[0] for i in sublist] [https://docs.python.org/2/tutorial/datastructures.html](https://docs.python.org/2/tutorial/datastructures.html)

> [@Crop Region by curve](https://forum.dynamobim.com/t/crop-region-by-curve/4607/20):
>
> Nice that you got it to work! If you wanted to combine the lists in python you could have used [zip()](https://docs.python.org/2/library/functions.html#zip) for view,curves in zip(views,curvelist): #Create curveloop loop = CurveLoop() for c in curves: loop.Append(c.ToRevitType()) #Set cropregion in Transaction TransactionManager.Instance.EnsureInTransaction(doc) view.CropBoxActive = True view.GetCropRegionShapeManager().SetCropShape(loop) outputlist.append(view) TransactionManager.Instance.TransactionTaskDone() OUT = outputlist

> [@Iterate over lists](https://forum.dynamobim.com/t/iterate-over-lists/4956/4):
>
> I am guessing you have 2 sublists and really what you’re asking is how can you perform cross product lacing with sublist 1 against sublist 2? Try this (this only works on a list with two sublists; if you are trying to match each item from sublist 1 with all items with sublist 2, then match each item from sublist 2 with all items from sublist 3…etc then this wont work and you should think about how to manage your data better) import clr clr.AddReference('ProtoGeometry') from Autodesk.DesignScri…

> [@How to Handle Variable Input List Length/Nesting in Custom Nodes (Python)](https://forum.dynamobim.com/t/how-to-handle-variable-input-list-length-nesting-in-custom-nodes-python/3192/19):
>
> The Konrad’s function using lists comprehension: elements = IN[0] ProcessLists = lambda function, lists: [ProcessLists(function, item) if isinstance(item, list) else function(item) for item in lists] sum = lambda x: x + 1 if isinstance(elements, list): out = ProcessLists(sum, elements) else: out = sum(elements) OUT = out [image]

> [@Applying Lineloads in Dynamo from element](https://forum.dynamobim.com/t/applying-lineloads-in-dynamo-from-element/15562/3):
>
> Try using a definition like so above your inputs… def tolist(obj1): if hasattr(obj1,“iter”): return obj1 else: return [obj1] And for the input beams write the following… beams = tolist(UnwrapElement(IN[0])) This is what I use for inputs to ensure they are an iterable object. Very common point of failure in writing python nodes. (I can’t seem to format the code on my mobile, but indented code after the first line naturally)

> [@Different ways of getting element Ids](https://forum.dynamobim.com/t/different-ways-of-getting-element-ids/7782/2):
>
> Hi @salvatoredragotta Both versions get the ElementId using the same method, which is the [.Id Property](http://www.revitapidocs.com/2015/9235095b-b7ae-b6e5-6cc2-2b8d397644de.htm) (any object that inherits from Element has it). What’s different, is what they do with the resulting ElementId objects retrieved. The method you use, will depend what you will do with the collection of ElementIds. More details below. The 2nd in your example, creates a list ([built-in Python list](https://docs.python.org/2.7/tutorial/datastructures.html)) which is a generic, container native to Python, the other creates a List, a .NET data-type. Pyt…

> <https://stackoverflow.com/questions/35515996/revit-api-dynamo-handling-variable-input-types>

---

_[View the full topic](https://forum.dynamobim.com/t/python-nodes-basics/15872)._
