# Why I can not change element of list in for cycle?

**URL:** <https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239>\
**Category:** Processing.py\
**Created:** [October 27, 2024, 6:09pm UTC](https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239 "2024-10-27T18:09:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elena](https://avatars.discourse-cdn.com/v4/letter/e/278dde/32.png) [@Elena](https://discourse.processing.org/u/Elena)\
**Post date:** [October 27, 2024, 6:09pm UTC](https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239/1 "2024-10-27T18:09:34Z")

</div>

Why elements of List is not changing? Python.

Yes, I know that I can use  
for m in range(0, len(circleList))  
and change circleList[m], and it is working fine for numbers.

I am tying to draw 2 moving cirles. If I use list of lists everything works fine:

```auto
circleList = [[100], [320]]
def setup():
      size(600,600)
    
def draw():
    background(147)
    global circleList
    for x in circleList:
        circle(x[0], 150, 50)
        x[0] = x[0] + 1
        if x[0] == 600:
            x[0] = -50

```

But it I use list of numbers, I do not see that element of list is changing:

```auto
circleList = [100, 320]
def setup():
    size(600,600)
    
def draw():
    background(147)
    global circleList
    for x in circleList:
        circle(x, 150, 50)
        x = x + 1
        if x == 600:
            x = -50

```

Why?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 27, 2024, 10:16pm UTC](https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239/2 "2024-10-27T22:16:12Z")

</div>

In both cases, _x_ is a local variable.

But in the 1st example, each _x_ points to a sublist from _circleList_; and you use the `[]` syntax to increment the 1st element of each sublist.

However, in the 2nd example, each _x_ points to an `int` number; and when you increment _x_, it creates a new `int` value which replaces that current `int` in _x_.

Given it’s creating a new `int` value (b/c `int` is immutable in Python) it’s not the same `int` that _circleList_ stores anymore.

While in the 1st example, _x_ is mutating each of the same sublists that list _circleList_ points to.

So for each iteration from the 1st example, _x_ temporarily becomes an alias (a different variable that points to the same object of another variable) to 1 of _circleList_’s sublists; 1st as an alias for _circleList[0]_, and 2nd as an alias for _circleList[1]_.

If you changed `x[0] = x[0] + 1` to `x = [x[0] + 1, x[1] ]`, _x_ would be re-assigned w/ a brand new list, whose changes would not be related to _circleList_’s own sublists whatsoever!

BtW, you can shorten `x[0] = x[0] + 1` to `x[0] += 1`; and `x = x + 1` to just `x += 1`.

---

<div class="post-metadata">

**Author:** ![Elena](https://avatars.discourse-cdn.com/v4/letter/e/278dde/32.png) [@Elena](https://discourse.processing.org/u/Elena)\
**Post date:** [October 28, 2024, 2:12am UTC](https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239/3 "2024-10-28T02:12:45Z")

</div>

Yes, I can see that a new local variable for x is creating, so when I print, it’s id will be different:

```auto
circleList = [100]
def setup():
    size(600,600)
    
def draw():
    noLoop()
    background(147)
    global circleList
    for x in circleList:
        circle(x, 150, 50)
        print(id(x)) #2
        x = x + 1
        print(id(x)) #3
        if x == 600:
            x = -50

```

Number in python is immutable

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 28, 2024, 2:30am UTC](https://discourse.processing.org/t/why-i-can-not-change-element-of-list-in-for-cycle/45239/4 "2024-10-28T02:30:19Z")

</div>

A bonus experiment:

```auto
circleList = [100, 320]

def setup():
    size(600, 600)
    frameRate(.5)
    
def draw():
    background(147)

    for i, x in enumerate(circleList):
        circle(x, 150, 50)

        x += 10

        print i, x, circleList[i] # circleList[] doesn't change!'

        if x == 600: x = -50

```
