# Sorting and Grouping List items with a reference nested list data structure

**URL:** https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484
**Category:** Developers
**Tags:** python, dynamo, list-filter
**Created:** [December 17, 2020, 6:45pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484 "2020-12-17T18:45:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![KiranGolla.BLOX](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@KiranGolla.BLOX](https://forum.dynamobim.com/u/KiranGolla.BLOX)
#### Post date: [December 17, 2020, 6:45pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/1 "2020-12-17T18:45:31Z")

</div>

I am trying to create a standardized method to sort and regroup a list of Dynamo Curves based on a nested list structure of Dynamo Points (_which were previously obtained from the same Curves_). A marked-up screenshot of the Dynamo Script is attached below for better understanding.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/7/0705940097f20d0d297cef64ea9f154b2a4abedd.jpeg)

My Current Script is below:

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

# Place your code below this line
########## Finding the MidPoints on Curve ##########
Pts = []
for i in Crvs:
	x = i.PointAtParameter(0.5)
	Pts.append(x)

########## Sorting the Crvs based on reference MidPts ##########
List = []
for i in MidPts:
	templst = []
	for j in i:
		for a in Pts:
			if j == a:
				y = Pts.index(a)
			templst.append(Crvs[y])
	List.append(templst)
	
# Assign your output to the OUT variable.
OUT = List

```

I am looking to either fix the current script or to figure out a better/efficient way to work.

Your help is very much appreciated!  
Thanks!!

---

<div class="post-metadata">

### Author: ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)
#### Post date: [December 17, 2020, 8:31pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/2 "2020-12-17T20:31:24Z")

</div>

The error gives you a pretty big clue. You’re only defining `y` when `j == a`, but you’re appending `Crvs[y]` regardless. You need to move the append function under your `if` indent.

That being said there is probably a much more direct way to get what you want. A better explanation of what you’re doing and what you’re trying to accomplish would help.

---

<div class="post-metadata">

### Author: ![KiranGolla.BLOX](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@KiranGolla.BLOX](https://forum.dynamobim.com/u/KiranGolla.BLOX)
#### Post date: [December 17, 2020, 9:30pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/3 "2020-12-17T21:30:34Z")

</div>

@Nick_Boyts  
What I am trying here might be a crude approach. As a Python beginner I would love to learn any efficient approach!

But my intent here is to reorganize the _‘Curves input list’_ by taking the list structure reference from the _‘MidPoints input list’_. The relation between the two inputs here is that each point in the _‘MidPoints input list’_ has a Curve from the _‘Curves input list’_ that has the same midpoint.

For better representation, please check the Inputs, known assumptions and desired Outputs in the code block below.

```
Crvs = [C1, C2, C3]
MidPts = [[P1,P3],[P2]]

# Below are the known assumptions
# cP1, cP2, cP3 are the midpoints of C1, C2, C3 respectively.
# cP1 = P1 , cP2 = P2 & cP3 = P3

# Intend Result/Output:
OUT = [[C1,C3],[C2]]
```

---

<div class="post-metadata">

### Author: ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)
#### Post date: [December 17, 2020, 9:48pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/4 "2020-12-17T21:48:54Z")

</div>

How are you getting these midpoints? It seems like you’re getting them and then reordering your curves when you could probably just get them in the correct order.

The best way is probably with a dictionary.

---

<div class="post-metadata">

### Author: ![KiranGolla.BLOX](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@KiranGolla.BLOX](https://forum.dynamobim.com/u/KiranGolla.BLOX)
#### Post date: [December 17, 2020, 10:46pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/5 "2020-12-17T22:46:09Z")

</div>

Yes, I just figured the same. I was able to get the results right using the dictionaries method in Python Online Compiler _(Screenshot below)_ but I am struggling to get the same implemented in the Dynamo IDE. ☹

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/1/b1506d9deac0c760bbe689507acd4d3d39c27fd0.png)

---

<div class="post-metadata">

### Author: ![KiranGolla.BLOX](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@KiranGolla.BLOX](https://forum.dynamobim.com/u/KiranGolla.BLOX)
#### Post date: [December 17, 2020, 11:00pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/6 "2020-12-17T23:00:06Z")

</div>

I think I figured it without the dictionaries, not sure if that’s the right way. 😕

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

---

<div class="post-metadata">

### Author: ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)
#### Post date: [December 18, 2020, 3:07pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/7 "2020-12-18T15:07:42Z")

</div>

I’m still wondering about how you’re getting your list of MidPts to begin with. I want to see the rest of your graph. You have a list of curves and you have a list of their midpoints. Why are they already not in the same order?

---

<div class="post-metadata">

### Author: ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)
#### Post date: [December 26, 2020, 2:28pm UTC](https://forum.dynamobim.com/t/sorting-and-grouping-list-items-with-a-reference-nested-list-data-structure/58484/8 "2020-12-26T14:28:41Z")

</div>

Hello  
to compare geometries (points) you can use:

- _geometry.DistanceTo(geometry)_  
**or**
- _geometry.IsAlmostEqualTo(geometry)_

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/2/b23c641d333d22e7c71382972a950f12fb4b92c7.jpeg)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/e/bec53cbe8e6046b2cc5d5aeaf33903f80a8afc4e.jpeg)
