# For looping in Jython?

**URL:** <https://forum.inductiveautomation.com/t/for-looping-in-jython/28405>\
**Category:** Ignition\
**Created:** [September 19, 2019, 6:54pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405 "2019-09-19T18:54:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 6:54pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/1 "2019-09-19T18:54:02Z")

</div>

```auto
machine = 'd'
number = {01,02,03,04}
for x in machine:
	for y in number:
		print (x,y)

```

returns

```auto
('d', 1)
('d', 2)
('d', 3)
('d', 4)

```

How would a guy return?

```auto
('d01')
('d02')
('d03')
('d04')

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 6:54pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/2 "2019-09-19T18:54:48Z")

</div>

How to concatenate ‘str’ and ‘int’ objects?

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [September 19, 2019, 7:04pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/3 "2019-09-19T19:04:30Z")

</div>

```auto
machine = 'd'
number = [01,02,03,04]
for x in machine:
	for y in number:
		print (str(x)+str(y))

```

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [September 19, 2019, 7:07pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/4 "2019-09-19T19:07:24Z")

</div>

The following forces two digits for `y` as in your example output:

```python
machine = 'd'
number = [01,02,03,04]
for x in machine:
	for y in number:
		print x + '%02d' % y

```

If you want the parentheses and quotation marks in the output, change the last line to:

```python
		print "('" + x + "%02d')" % y

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 7:18pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/5 "2019-09-19T19:18:55Z")

</div>

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
params = {"TBC":0,
		"TGC":0,
		"TPC":0,
		"RCT":0,
		"GHS":0, 
		"TS":time.value,
		"TBLNum":group}
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
	for y in number:
		group = (str(x) + str(y))
		for i in group:
			print params	

```

returns

```auto
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}

```

Why does this print 12 times?  
Why is it always passing d99?

How would one return the following?

```auto
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 7:29pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/6 "2019-09-19T19:29:03Z")

</div>

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
	for y in number:
		group = (str(x) + str(y))
		params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time.value,"TBLNum":group}
		for group in params:
			print params	

{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd96', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd97', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 14:00:00 CDT 2019}

```

LOL getting closer

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 7:36pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/7 "2019-09-19T19:36:01Z")

</div>

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machine = 'd'
number = {99,98,97,96}
for x in (machine):
	for y in (number):
		group = (str(x) + str(y))
		params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time.value,"TBLNum":group}
		for group in params:
			print params
			break		

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 19, 2019, 7:45pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/8 "2019-09-19T19:45:48Z")

</div>

You were close, try this.

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry").value
machine = 'd'
number = [99,98,97,96]
for x in (machine):
	for y in (number):
		group = (str(x) + str(y))
		params = {"TBC":0,"TGC":0,"TPC":0,"RCT":0,"GHS":0,"TS":time,"TBLNum":group}
		print params

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 19, 2019, 7:47pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/9 "2019-09-19T19:47:10Z")

</div>

Or similar, cleaning up the variable names a bit:

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machines = ["d"]
machine_numbers = [99, 98, 97, 96]

params = []

for machine in machines:
    for number in machine_numbers:
        params.append(
            {
                "TBC": 0,
                "TGC": 0,
                "TPC": 0,
                "RCT": 0,
                "GHS": 0,
                "TS": time.value,
                "TBLNum": "%s%s" % (machine, number),
            }
        )

for group in params:
    print group

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 19, 2019, 7:48pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/10 "2019-09-19T19:48:38Z")

</div>

One important thing to note: `number = {99,98,97,96}` creates `number` as a _set_, which is an **unordered** sequence. If the final output order matters, you should use a list (`[]`) or tuple (`()`) instead of the set initializer (`{}`).

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [September 19, 2019, 9:06pm UTC](https://forum.inductiveautomation.com/t/for-looping-in-jython/28405/11 "2019-09-19T21:06:13Z")

</div>

> [@paul-griffith](#):
>
> "TBLNum": "%s%s" % (machine, number),

The order doesn't matter in this case. Thanks for this example! It helps connect some dots for me....This is like string formatting in C. Python is hella wild.

This script for loops through a named query params arg if there are any future readers. Your mileage may vary.

```auto
time = system.tag.read("[default]SCADA/Time/Clean Time Entry")
#list of machine tables in db
machines = ["d"]
machine_numbers = [01,02,03,04,05,06,07,08,09,10,11,12,13,14,16,17,19,21,22,23]
nq = 'New Folder/clean'
params = []

for machine in machines:
    for number in machine_numbers:
        params.append(
            {
                "TBC": 0,
                "TGC": 0,
                "TPC": 0,
                "RCT": 0,
                "GHS": 0,
                "TS": time.value,
                "TBLNum": "%s%s" % (machine, number),
            }
        )

for group in params:
    print group
    system.db.runNamedQuery(nq, group)
    system.db.clearNamedQueryCache(nq)

```

```auto
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd99', 'TS': Thu Sep 19 15:00:00 CDT 2019}
1
{'RCT': 0, 'TPC': 0, 'TGC': 0, 'GHS': 0, 'TBC': 0, 'TBLNum': 'd98', 'TS': Thu Sep 19 15:00:00 CDT 2019}
1

```
