# Function combinations(iterable, r)

**URL:** <https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640>\
**Category:** Ignition\
**Created:** [April 9, 2018, 9:34am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640 "2018-04-09T09:34:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [April 9, 2018, 9:34am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640/1 "2018-04-09T09:34:29Z")

</div>

Greetings

Was trying to find out how to implement the coding to get all the combinations that I could get from 4 items.

Had searched through and found that in python there is such a function (itertools.combinations) for this purpose but when browse through the IA forum and seems to get the impression that the itertools library would not be imported.

Nevertheless, had tried out as follows from the python org ([https://docs.python.org/2/library/itertools.html#itertools.combinations](https://docs.python.org/2/library/itertools.html#itertools.combinations)) and it didnt work as i kept getting the message “generator object at 0xa”  
"generator object at 0xb " instead of what is to be expected "AB AC AD BC BD CD ".

Wonder what is missing or need to be corrected ?

Thank you

def combinations(iterable, r):  
# combinations(‘ABCD’, 2) --\> AB AC AD BC BD CD  
# combinations(range(4), 3) --\> 012 013 023 123  
pool = tuple(iterable)  
n = len(pool)  
if r \> n:  
return  
indices = range®  
yield tuple(pool[i] for i in indices)  
while True:  
for i in reversed(range®):  
if indices[i] != i + n - r:  
break  
else:  
return  
indices[i] += 1  
for j in range(i+1, r):  
indices[j] = indices[j-1] + 1  
yield tuple(pool[i] for i in indices)

print combinations(‘ABCD’, 2)  
abc = combinations(‘ABCD’, 2)  
print abc

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [April 9, 2018, 9:53am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640/2 "2018-04-09T09:53:44Z")

</div>

Could you please print your code in a code field? Like this, the indentation is lost, which is very important for Python.

That said, the output shows you’re getting back a generator object. A generator can’t be printed, as nothing is calculated yet. To print from a generator, you should loop through it, or f.e. cast it to a list and let the list creator loop throug it.

try `print list(combinations'ABCD', 2))` instead

---

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [April 9, 2018, 10:05am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640/3 "2018-04-09T10:05:06Z")

</div>

> [@Sanderd17](#):
>
> print list(combinations'ABCD', 2))

Thanks a lot , it works

beg your pardon for the lost in the indentation and i know it is important for python but sorry to ask where is the code field?

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [April 9, 2018, 10:31am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640/4 "2018-04-09T10:31:12Z")

</div>

Either use backticks, or click on the “preformatted text” button.

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/9bf5532612400efe3a709e9ad52087b6882eb6be.png)

---

<div class="post-metadata">

**Author:** ![mic](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@mic](https://forum.inductiveautomation.com/u/mic)\
**Post date:** [April 9, 2018, 10:37am UTC](https://forum.inductiveautomation.com/t/function-combinations-iterable-r/17640/5 "2018-04-09T10:37:37Z")

</div>

Noted with great Thanks
