How do variables work? (Boredless Tourist project)

The exercise I’m working on is the Boredless Tourist project. Here’s the relevant code:

attractions = [[] for i in range(0, 5)]

def add_attraction(destination, attraction):
  destination_index = get_destination_index(destination)
  try:
    attractions_for_destination = attractions[destination_index]
    attractions_for_destination.append(attraction)
    return
  except ValueError:
    return

add_attraction("Los Angeles, USA", ["Venice Beach", ["beach"]])

print(attractions)

The code is working correctly. When I print attractions, it’s been updated with the Venice Beach attraction information.

My problem is that I don’t understand why the “attractions” variable was updated when I appended the attraction data to a whole different variable called attractions_for_destination.

I seem to be missing some important concept about how variables work (namely how and why attractions got updated when I appended data to a different variable. Can someone explain?

This line

attractions_for_destination = attractions[destination_index]

… could be omitted (as all it does is set up an intermediate variable), in which case, the next line would become:

attractions[destination_index].append(attraction)

… which directly modifies (mutates) the list, attractions.

1 Like

Thank you for your reply, patrick! I have a slightly better understanding now and I appreciate it!

I’m still a little confused about the intermediary concept. How (or why) do changes made to attractions_for_destination show up in attractions?

Here's what I'm expecting

x = 1
y = x
y += 1
result:
x = 1
y = 2
(x remains the same, y changes)

Instead it seems to be
result:
x = 2
y = 2
(even though I incremented y, somehow x got updated too)

That seems to be happening here and I’m confused as to when/where/why/how it’s doing that.

1 Like

I’ve figured this out! The behavior I’m noticing is specific to lists (from a different tutorial site):

Note that Python creates a single new list every time you execute the [ ] expression. No more, no less. And Python never creates a new list if you assign a list to a variable.

A = B = [ ] # both names will point to the same list
A = [ ] B = A # both names will point to the same list
A = [ ]; B = [ ] # independent lists

In the Boredless Tourist project, the variable attractions_for_destination is merely an alias for attractions[destination_index]. Either variable can be used to access, change, update the same list.

I’ve re-read the lessons about lists and I don’t see this mentioned anywhere. Did I miss it? It’s pretty important information to know!

2 Likes

Yes, exactly! :slightly_smiling_face:

And, no, I don’t think that anything having to do with variable assignment in Python, certainly including its application to lists, is well covered, if at all, in the course material. As time goes on, it is to be expected that we will all need to “ask Dr. Google” about more and more, but this is pretty basic, and should have been covered.

1 Like

To be honest, even in the official Python documentation, I think the only mention of that behaviour is this:

Assignment statements in Python do not copy objects, they create bindings between a target and an object.

…and that’s buried in the docs for the copy functionality.

https://docs.python.org/3.6/library/copy.html

So, probably could be made clearer. :slight_smile:

1 Like

For sure!

Here is the best explanation I’ve seen on variable assignment.

… and here (from the same tutorial) on copying.