# "Item;List;Tree" Access in GhPython

**URL:** <https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968>\
**Category:** Scripting\
**Tags:** ghpython\
**Created:** [September 20, 2020, 5:25am UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968 "2020-09-20T05:25:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [September 20, 2020, 5:25am UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/1 "2020-09-20T05:25:42Z")

</div>

Hello  
Is there a way to change between **Item;List;Tree … Access** from GhPython?

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/e/eea67dc773dede6d23fd1f4d0fbdc5e158f71300.png)

---

<div class="post-metadata">

**Author:** ![Mahdiyar](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mahdiyar/32/472862_2.png) [@Mahdiyar](https://discourse.mcneel.com/u/Mahdiyar)\
**Post date:** [September 20, 2020, 5:46am UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/2 "2020-09-20T05:46:33Z")

</div>

![ParamAccess](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/2/b/2ba1319edcce014e75bf1a4621d284a082cf0842.gif)

```python
from Grasshopper.Kernel import GH_ParamAccess
if x == 0:
    access = GH_ParamAccess.item
if x == 1:
    access = GH_ParamAccess.list
if x == 2:
    access = GH_ParamAccess.tree
if (access != ghenv.Component.Params.Input[1].Access):
    ghenv.Component.Params.Input[1].Access = access
    ghenv.Component.ExpireSolution(True)

```

[ParamAccess.gh](https://discourse.mcneel.com/uploads/short-url/zxojQBijMc5fNlkEpr0StgLONsl.gh) (9 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [September 20, 2020, 6:56am UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/3 "2020-09-20T06:56:16Z")

</div>

Thank you @Mahdiyar , unfortunately didn’t work with GH\_ParamAccess(x); i make a little change and i believe with enum will be simpler  
and this change of the used component , it will need it for other things but in this case i want switch between item/list/tree inside ghpython.  
the idea is to group similar items and i need to show list from **IronPython.Runtime.List** without lose data tree  
The second python component can do that by choose item access and use a = x, but i want to do that in the same script, that’s why i want switch between them if it is possible

```
from Grasshopper.Kernel import GH_ParamAccess

access = [GH_ParamAccess.item,GH_ParamAccess.list,GH_ParamAccess.tree]

if access[x] != ghenv.Component.Params.Input[1].Access:
    ghenv.Component.Params.Input[1].Access = access[x]
    ghenv.Component.ExpireSolution(True)  

```

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/9/c/9cebffb0c671e4d53d7f4e4e8204d6a1e143edbb.png)

[group.gh](https://discourse.mcneel.com/uploads/short-url/eXl0rlYuNZvTByi8Vn9EwGs4RAF.gh) (9.5 KB)

---

<div class="post-metadata">

**Author:** ![Bogdan\_Chipara](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/bogdan_chipara/32/476579_2.png) [@Bogdan\_Chipara](https://discourse.mcneel.com/u/Bogdan_Chipara)\
**Post date:** [September 20, 2020, 2:11pm UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/4 "2020-09-20T14:11:22Z")

</div>

Hi @anon39580149,  
maybe something like below?

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/4/f/4fe393be803981ec753fd3039614e6c657d32aae.png)

And here’s my code:

```
"""Provides a scripting component.
    Inputs:
        x: The x script variable
        y: The y script variable
    Output:
        a: The a output variable"""

__author__ = "bogdan"
__version__ = "2020.09.20"

import rhinoscriptsyntax as rs
from itertools import groupby
from itertools import chain
import ghpythonlib.treehelpers as th

total = 0
listChunks = []
second_list =y

for j in range(len(second_list)):
    chunk_mylist = x[total:total+second_list[j]]
    listChunks.append(chunk_mylist)
    total += second_list[j]
#After running this, listChunks is a list containing sublists with the lengths found in second_list.

#print listChunks

endlist =[]
for j in range(len(listChunks)):
    lists2 = [list(listChunks[j]) for i, listChunks[j] in groupby(listChunks[j])]
    print lists2
    endlist.append(lists2)

#a= th.list_to_tree(listChunks)
b= th.list_to_tree(endlist)

```

[200920BC preserve tree structure.gh](https://discourse.mcneel.com/uploads/short-url/3xRK081YQL1Y4FbD44kACCoIctf.gh) (10.0 KB)

---

<div class="post-metadata">

**Author:** ![anon39580149](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@anon39580149](https://discourse.mcneel.com/u/anon39580149)\
**Post date:** [September 20, 2020, 2:46pm UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/5 "2020-09-20T14:46:40Z")

</div>

Thank you @Bogdan_Chipara for help, i definitively learn from it.  
I already found this solution

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/0/609565170703238d559163e0ff5eae6d93857df2.png)

---

<div class="post-metadata">

**Author:** ![Bogdan\_Chipara](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/bogdan_chipara/32/476579_2.png) [@Bogdan\_Chipara](https://discourse.mcneel.com/u/Bogdan_Chipara)\
**Post date:** [September 20, 2020, 2:48pm UTC](https://discourse.mcneel.com/t/item-list-tree-access-in-ghpython/108968/6 "2020-09-20T14:48:23Z")

</div>

Cool, yours is more elegant! I will also learn from it.
