# Counting List Levels and Items at those Levels

**URL:** <https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299>\
**Category:** Lists-Logic\
**Created:** [January 19, 2018, 1:45am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299 "2018-01-19T01:45:23Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 19, 2018, 1:45am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/1 "2018-01-19T01:45:23Z")

</div>

Hi All,

Quick one I hope. 😀  
First week back and the brain has gone fuzzy.

I am trying to count the number of list levels in a list and the number of items on those levels.

Doe’s any one have a suggestion on how to get my designscript value (the -2 circled in blue) to take a multi item list (circled in red) to effectively perform the actions circled in green?

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

A straight input swap to try and add items from another list gives this error.

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/e/deee457d55c49f96edb55e5fe3d5e6332d5f3681.png)

There is probably a super simple python fix for this… but thought Id try DS first 😉  
Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [January 19, 2018, 5:23am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/2 "2018-01-19T05:23:21Z")

</div>

Not sure what you’re looking for in the end, but maybe this is the code you’re looking for?

`Count(a@@-2<1L>);`

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [January 19, 2018, 6:55am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/3 "2018-01-19T06:55:24Z")

</div>

@Ewan_Opie Alternately try `List.Count(a<1><2>) ;`

 ![Count](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/b/2bf970c273f11b127423ac7b80ecf2d54e56a6b3.png)

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 19, 2018, 8:30am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/4 "2018-01-19T08:30:46Z")

</div>

Thanks for the responses @jacob.small and @Vikram_Subbaiah

I am looking to map the structure of the list.

So i need to determine how many levels the list contains before extracting the count at those levels. And I’m trying to make it dynamic, so lists of any structure/number of levels can be mapped.

Im trying a sort of fractal mapping… 🤔

---

<div class="post-metadata">

**Author:** ![Thomas\_Mahon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_mahon/32/64209_2.png) [@Thomas\_Mahon](https://forum.dynamobim.com/u/Thomas_Mahon)\
**Post date:** [January 19, 2018, 8:40am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/5 "2018-01-19T08:40:45Z")

</div>

List.Rank will tell you how many levels a list has (its depth). If you are dealing with jagged arrays it gets more complicated as only the deepest rank will be returned.

If you not only need to know the deepest rank but also the count of items at each level then a recursive function (Python or DS for example) would be the easiest solution.

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 19, 2018, 9:05am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/6 "2018-01-19T09:05:03Z")

</div>

Thanks @Thomas_Mahon  
I thought as much, no simple substitutes here.  
I’ll mull it over this weekend and get back into it Monday. 👍🏻

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 22, 2018, 12:19am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/7 "2018-01-22T00:19:19Z")

</div>

Might be a wasted thought, but is there any way of inputting strings to “write” code into code blocks?

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 22, 2018, 1:15am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/8 "2018-01-22T01:15:06Z")

</div>

So starting on the recursion and stumbling when I get to the same step.

I am wanting to replicate changing the list levels on the count node, multiple times for the same input list to get the different item numbers at each level.

Simple counting at some levels recursively works but what do i need to modify to add in multiple levels (Count(list@-1\<1\>, Count(list@-2\<1\> … ) Straight parameter swapping throws the same error as before. Thoughts?

 ![ezgif-1-41cd362f4b](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/1/91b5bcc6f0683f2d958c8e7c90c1ff03bee8ab6c.gif)

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [January 22, 2018, 6:04am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/9 "2018-01-22T06:04:34Z")

</div>

Still trying to work on a way to make it work for any size list but I got a python script to work so far. I want to make a function to do the for-if-counting for me so I will keep working on it for now.

 ![listcounting](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/4/c4315b0980c54357131b24360fb6ec930b7ed00d.PNG)

```
#The inputs to this node will be stored as a list in the IN variables.
theList = IN[0]

def depth(nested):
	instring = False
	count = 0
	depthlist = []
	for char in repr(nested):
	    if char == '"' or char == "'":
	        instring = not instring
	    elif not instring and ( char == "[" or char == ")" ):
	        count += 1
	    elif not instring and ( char == "]" or char == ")" ):
	        count -= 1
	    depthlist.append(count)
	return(max(depthlist))

counts = [0]*depth(theList)

for L1 in range(len(theList)):
	if type(theList[L1]) == list:
		for L2 in range(len(theList[L1])):
			if type(theList[L1][L2]) == list:
				for L3 in range(len(theList[L1][L2])):
					if type(theList[L1][L2][L3]) == list:
						counting = len(theList[L1][L2][L3])
						counts[3] += counting
				counting = len(theList[L1][L2])
				counts[2] += counting
		counting = len(theList[L1])
		counts[1] += counting
counting = len(theList)
counts[0] += counting

OUT = counts

```

Edit: I think I misunderstood the question. My method counts how many elements are at each level across all of the lists. Did you want to count how many elements are at each level categorized based on each nested list?

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 22, 2018, 6:36am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/10 "2018-01-22T06:36:55Z")

</div>

Yes, to the later. Thanka for looking into this, great python example. 👍🏻

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [January 22, 2018, 7:00am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/11 "2018-01-22T07:00:20Z")

</div>

Using the list you have in your example:  
`0..3..(1..5..(1..2..(1..6)));`  
Can you show what you want as the output? Kind of confused as to what you want shown.

 ![listindex](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/c/5c12855f9b2c3f6c7c58378ca589e06c56f0a140.PNG)  
Here is a way to get each one using OOTB nodes, just don’t know how you want to combine them.

---

<div class="post-metadata">

**Author:** ![oliver.green](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/oliver.green/32/22443_2.png) [@oliver.green](https://forum.dynamobim.com/u/oliver.green)\
**Post date:** [January 22, 2018, 9:08am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/12 "2018-01-22T09:08:42Z")

</div>

For a list of arbitrary (i.e. any) depth, you’ll need to define a recursive function.  
At a certain point, iteration, such as for and while loops, stops making sense.

---

<div class="post-metadata">

**Author:** ![kennyb6](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kennyb6](https://forum.dynamobim.com/u/kennyb6)\
**Post date:** [January 22, 2018, 9:22am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/13 "2018-01-22T09:22:53Z")

</div>

Yeah, that’s what I was trying to say. All I wanted to do with that was try to find what he wants as the outcome for the script, using the input he had. Then I could try writing a function to do it for any depth.

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 22, 2018, 8:20pm UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/14 "2018-01-22T20:20:19Z")

</div>

This structure of list would be the end result.

 ![ezgif-1-f47795900b](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/f/0f2d53b7edcdfaed28ba6b83fd8afae5c46eb6d5.gif)

Function would be:

1. Find the number of levels in the list (automate d function)
2. For each of those levels count the number of items (tricky part)
3. Compile that to a single list (no other user input)

Is that clearer?

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [January 23, 2018, 7:09am UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/15 "2018-01-23T07:09:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [January 23, 2018, 8:00pm UTC](https://forum.dynamobim.com/t/counting-list-levels-and-items-at-those-levels/18299/16 "2018-01-23T20:00:59Z")

</div>

Nailed it @Dimitar_Venkov 👍 The legend strikes again!  
Thanks to all others for their input to. I will put this into a worked example of what I am trying to achieve and post back here. 😀
