pythontuplescoordinatesnested-listspoints

How do I change an element in a nested list?


I am a Python beginner and in need of some advice.

I defined ten variables that all have an X and Y value.

Then I put these variables (which are actually a list) in another list called "pointlist".

Now, I want to use a for-loop to change the X-value of each variable.

Usually, I can access the X-value of the first variable through "pointlist[0][0]".

When I applied this to the for-loop I got the following error: "TypeError: list indices must be integers or slices, not tuple"

Does anyone have an idea what I missed? Thank you so much in advance!

import random 
X=1350
Y=250

p1=[X, Y]
p2=[X, Y]
p3=[X, Y]
p4=[X, Y]
p5=[X, Y]
p6=[X, Y]
p7=[X, Y]
p8=[X, Y]
p9=[X, Y]
p10=[X, Y]
p11=[X, Y]
pointlist = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11]

limit1=1150
limit2=1250

for i in enumerate (pointlist, start=1):
    pointlist[i][0] = random.randrange(limit1, limit2)
    limit1-=100
    limit2-=100

Solution

  • Pythonize your loop using:

    for i in pointlist:
        i[0] = random.randrange(limit1, limit2)
        limit1-=100
        limit2-=100
    

    This way i becomes an entry of pointlist for easy access