how to write print output within a for loop to a list in python?

Issue

I have put all of my code for context. Basically it should print a list of vectors depending on which route the user selects. As it stands it prints the vectors to the screen like this:

[12, 2]
[12, 3]
[11, 3]
...
...

which is what i need, however, i also want the printed vectors to be written to a list in the order they are printed so i can call on them later on i.e. vector_list = [[12, 2], [12, 3], 11, 3]]….

I am very new to this and learning as I go so thank you for your patience. Below will create a list called vector_list but it will just save the same vector i.e. [12, 2] over and over again. I think my issue is i am using .append? I originally wanted to try something like vector_list = vector_list + x but it says vector_list is not defined?

  else:
        print(start)
        vector_list.append(start)

vectors()
print(vector_list)    

The complete code for reference:

route001 = (3, 12, 'S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'S', 'W',
            'W', 'S', 'E', 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'W', 'N', 'N',
            'E', 'E', 'S', 'E', 'S', 'E', 'S', 'S', 'W', 'S', 'S', 'S', 'S',
            'S', 'E', 'N', 'E', 'E')

route002 = (12, 11, 'W', 'W', 'S', 'S', 'S', 'W', 'W', 'N', 'N', 'N', 'W', 'W',
        'W', 'S', 'S', 'S', 'S', 'E', 'E', 'S', 'W', 'W', 'W', 'W', 'N', 'N',
        'W', 'W', 'S', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'E', 'S', 'E', 'S')

route003 = (3, 12, 'S', 'S', 'W', 'S', 'S', 'S', 'W', 'W', 'W', 'S', 'S', 'W',
        'W', 'S', 'E', 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'W', 'N', 'N', 'E',
        'E', 'S', 'E', 'S', 'E', 'S', 'S', 'W', 'S', 'S', 'S', 'S', 'S', 'E',
        'N', 'E', 'E')



route_selection = input('Select route 1, 2 or 3 ')
if route_selection == "1":
    selectedRoute = route001
elif route_selection == "2":
    selectedRoute = route002
elif route_selection == "3":
    selectedRoute = route003
else:
    print("error")

start = [selectedRoute[0]] + [selectedRoute[1]]
directions = selectedRoute[2:]

coordinates = {"N": [0, 1], 'E': [1, 0], 'S': [0, -1], 'W': [-1, 0]}
vector_list = []

def vectors():

    for d in directions:
        dx, dy = coordinates[d]
        start[0] += dx
        start[1] += dy
        if start[0] < 0 or start[0] > 12:
            print('Error: This route goes outside the grid')
            break
        elif start[1] < 0 or start[1] > 12:
            print('Error: This route goes outside the grid')
            break
        else:
            print(start)
            vector_list.append(start)

vectors()
print(vector_list)

Solution

The problem is list is a mutable object in Python (I assume you don’t know what that is, I am encouraging you to Google it:)

Basically that means, when you append you list to the "list of vectors", it does NOT make a copy of that list and stores in the list of vectors, it (very roughly speaking, not that accurate) keeps a pointer to the list and stores it in the list of vectors

When you change your list, all the entries in the list of vectors change, because they are pointing to the same list you append

So, what you could do, for example, is make a copy of that list and store it inside "list of vectors"

Just change this line:
vector_list.append(start)

To:
vector_list.append(start.copy())

And it should work, there might be a more efficient way of doing it.

Answered By – Guy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published