# Python update variable name in for loop

**URL:** <https://community.unix.com/t/python-update-variable-name-in-for-loop/353736>\
**Category:** Shell Programming and Scripting\
**Created:** [July 1, 2015, 2:44pm UTC](https://community.unix.com/t/python-update-variable-name-in-for-loop/353736 "2015-07-01T14:44:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zam\_1234](https://community.unix.com/letter_avatar/zam_1234/32/6_22734026db12803ebb3e059622ac3534.png) [@Zam\_1234](https://community.unix.com/u/Zam_1234)\
**Post date:** [July 1, 2015, 2:44pm UTC](https://community.unix.com/t/python-update-variable-name-in-for-loop/353736/1 "2015-07-01T14:44:37Z")

</div>

Hello all,  
Not sure if this question has been answered already.

I have some xml Element variable as below:

```nohighlight
child19 = core_elem_dcache.find('stat[@name="read_accesses"]')
    child20 = core_elem_dcache.find('stat[@name="write_accesses"]')
    child21 = core_elem_dcache.find('stat[@name="read_misses"]')
    child22 = core_elem_dcache.find('stat[@name="write_misses"]'

```

Next I want to change the values of these corresponding elements like below:

```nohighlight
child20.set('value', str(params[19]))
child21.set('value', str(params[20]))
child22.set('value', str(params[21])) ######params is a list already read before from a text file

```

Now instead of setting setting the values manually for each element, I wanted to do a for loop for a range (10,100). But I am not sure how to update the "child" element dynamically.

I tried following:

```nohighlight
for x in range(13,23):
        child{x}.set('value',str(params[x-1]))

```

which surely gave me error:

```nohighlight
File "abc.py", line 99
    child{x}.set('value',str(params[x-1]))
         ^
SyntaxError: invalid syntax

```

Kindly help.

---

<div class="post-metadata">

**Author:** ![cjcox](https://community.unix.com/user_avatar/community.unix.com/cjcox/32/1232_2.png) [@cjcox](https://community.unix.com/u/cjcox)\
**Post date:** [July 2, 2015, 1:28pm UTC](https://community.unix.com/t/python-update-variable-name-in-for-loop/353736/2 "2015-07-02T13:28:58Z")

</div>

Check your python documentation for the 'exec' function. if you can't use child[index] and have to use hard names, exec is your friend for creating on the fly code (normally you would not do this though.. but there are some cases...).

---

<div class="post-metadata">

**Author:** ![jethrow](https://community.unix.com/letter_avatar/jethrow/32/6_22734026db12803ebb3e059622ac3534.png) [@jethrow](https://community.unix.com/u/jethrow)\
**Post date:** [July 3, 2015, 4:05pm UTC](https://community.unix.com/t/python-update-variable-name-in-for-loop/353736/3 "2015-07-03T16:05:22Z")

</div>

Instead of using sequentially named variables, why not use a list (if you don't need to start with 19), or a dict?

```nohighlight
child = {}
child[19] = 'value 19'
child[20] = 'value 20'

for x in range(19,20):
    child[x] = 'another value ' + str(x)

```
