# Finding if the input is list or not using python

**URL:** <https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188>\
**Category:** Revit\
**Tags:** python, dynamo\
**Created:** [April 18, 2024, 5:45am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188 "2024-04-18T05:45:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![siddhartharajendran9](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@siddhartharajendran9](https://forum.dynamobim.com/u/siddhartharajendran9)\
**Post date:** [April 18, 2024, 5:45am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/1 "2024-04-18T05:45:16Z")

</div>

Hi,

Thanks in advance for your effort and time

I’m trying to find if the input is list or not, and if it is not a list the python will convert into a list.  
When I pass a input which is non list, it is converting into a proper list, but when I pass list, the python is converting the same list into another list. I have attached snip and code below

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

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

_ **Python Code:** _

```auto
def tolist(obj):
	result = input if isinstance(obj, list) else [obj]
	return result

element = tolist(UnwrapElement(IN[0]))
OUT = element

```

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [April 18, 2024, 6:45am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/2 "2024-04-18T06:45:32Z")

</div>

@siddhartharajendran9 ,

it works well with this snippet

 ![grafik](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/6/06910b667de9fcac03fc41947a8c39786e5bdb75.png)

```auto
# 🔑 function
def tolist(x):
	if hasattr(x,' __iter__'): return x
	else: return [x]

# 🎯 items
element = tolist(IN[0])

OUT = element

```

---

<div class="post-metadata">

**Author:** ![siddhartharajendran9](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@siddhartharajendran9](https://forum.dynamobim.com/u/siddhartharajendran9)\
**Post date:** [April 18, 2024, 6:49am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/3 "2024-04-18T06:49:08Z")

</div>

Working fine now, thank you so much

---

<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 18, 2024, 8:12am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/4 "2024-04-18T08:12:36Z")

</div>

I think the first approach should work but input should be obj. I use it in my nodes as strings are iterable so believe the alt method might have issues there.

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [April 18, 2024, 9:39am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/5 "2024-04-18T09:39:31Z")

</div>

> [@siddhartharajendran9](#):
>
> `result = input if isinstance(obj, list) else [obj]`

(following Gavin’s comment)  
`input` is a built-in function in Python - update to this  
`result = obj if isinstance(obj, list) else [obj]`

@GavinNicholls strings ~~use the ` __getitem__ ` method to act as an iterable sequence so they~~ return ~~false~~ true for `hasattr("this is a string", " __iter__")`

**Edit** @haganjake2 You are correct - TIL strings do have an ` __iter__ ` method

---

<div class="post-metadata">

**Author:** ![haganjake2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/haganjake2/32/119069_2.png) [@haganjake2](https://forum.dynamobim.com/u/haganjake2)\
**Post date:** [April 18, 2024, 10:31am UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/6 "2024-04-18T10:31:03Z")

</div>

Annoyingly in CPython 3 it doesnt @Mike.Buttery.

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

Not as concise but if you want to make sure you account for strings being iterable you can add this:

```auto
def tolist(obj1):
    if isinstance(obj1, str): return [obj1]
    elif hasattr(obj1, " __iter__"): return obj1
    else: return [obj1]

```

---

<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:** [April 18, 2024, 12:54pm UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/7 "2024-04-18T12:54:24Z")

</div>

Not at my PC, but I think this is my method of choice for CPython list enforcement:

```auto
data = IN[0]
if data. __class__!= list: data = [data]

```

If I typed it correctly this works in CPython3 and IronPython2. Not sure abouit IronPython3 at the moment though.

---

<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:** [April 18, 2024, 5:41pm UTC](https://forum.dynamobim.com/t/finding-if-the-input-is-list-or-not-using-python/100188/8 "2024-04-18T17:41:44Z")

</div>

For info, `hasattr(x, " __iter__")` is currently buggy with CPython3/PythonNet2 with some .Net object

an example

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

another solution (works for both engines)

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