-1

Apologies for the basic question, I'm new to Python & struggling to find an answer.

How do I execute the below if-statement for each item in the list loaded_route?

i.e. check the first loaded_route value, update location, then check the next value, update location etc.

My current for-loop compares the entire list, not individual values. Thanks!

location = [1,1]
loaded_route = ['right', 'left']
route_coords = []

for route in loaded_route:
    if route == 'right':
        location[0] = (location[0] + 1)
        route_coords.append(location)
    elif route == 'left':
        location[0] = (location[0] - 1)
        route_coords.append(location)
    elif route == 'up':
        location[1] = (location[1] - 1)
        route_coords.append(location)
    elif route == 'down':
        location[1] = (location[1] + 1)
    else:
        print('failed to read loaded_route')
RSGEO
  • 33
  • 6
  • 1
    Are you sure it's _comparing_ the entire list? Have another look at what your `append`ing to your `route_coords` ;) – Zoltán Sep 27 '22 at 17:34
  • 2
    I'm confused. That loop does what you're describing, unless you're describing it wrong (no offense, we all start somewhere). Please show us your desired output and actual output. Like, maybe you want to `print(location)` each loop? It's not clear. For more tips, see [ask]. – wjandrea Sep 27 '22 at 17:35
  • 3
    Oh, are you looking at `route_coords` at the end and seeing all the values are the same? That's working as expected since they're all the same object. If you want to copy `location` each time you append it, there's an existing question: [How do I clone a list so that it doesn't change unexpectedly after assignment?](/q/2612802/4518341) – wjandrea Sep 27 '22 at 17:40
  • Well don't I feel silly... Thanks Zoltán & wjandrea for helping me out so fast - not a mistake I'll make again in a hurry! – RSGEO Sep 27 '22 at 17:43
  • Possible duplicate: [Why does foo.append(bar) affect all elements in a list of lists?](/q/6360286/4518341) – wjandrea Sep 27 '22 at 17:45

1 Answers1

1

location is type list, and lists are "mutable" in Python. That means that every time you add location to route_coords, you are merely inserting a reference to location, which is allowed to keep changing.

Instead, you want to make a copy of the current location value to route_coords.

location = [1,1]
loaded_route = ['right', 'left']
route_coords = []

for route in loaded_route:
    if route == 'right':
        location[0] = (location[0] + 1)
        route_coords.append(location[:])
    elif route == 'left':
        location[0] = (location[0] - 1)
        route_coords.append(location[:])
    elif route == 'up':
        location[1] = (location[1] - 1)
        route_coords.append(location[:])
    elif route == 'down':
        location[1] = (location[1] + 1)
        route_coords.append(location[:])
    else:
        print('failed to read loaded_route')
nofinator
  • 2,906
  • 21
  • 25
  • Thanks a lot for helping so quickly, that's exactly what I was looking for but didn't know how to word my search for [:]. – RSGEO Sep 27 '22 at 17:45