Boredless Tourist: Help understanding the add_attraction function

Hi there,

I finished the Borderless Tourist, and it works! However, I’m not 100% confident I understand how this function is working. The purpose of it, is to take in a destination and an attraction and add that attraction to the attractions list in the right index, per the destination:

def add_attraction(destination, attraction):
  try:
    destination_index = get_destination_index(destination)
    
    attractions_for_destination = attractions[destination_index]
    
    attractions_for_destination.append(attraction)
    return
    
  except ValueError: print("Destination doesn't exist!")
  return
  • destination_index takes destination as an argument and returns that destinations index in the attractions list–which has an index of 0-4, in which I store destinations per city.
  • attractions_for_destination takes the destinations stored in the attractions list, at the index that was retrieved
  • I then append the attraction that is passed to this add_attraction function to the attractions_for_destination variable, and return.

At what point does the attraction/destination combo get stored into the attractions list?

1 Like

This exercise uses parallel lists. destinations is a list of strings, and attractions is a list of lists, having the same number of lists as destinations has strings.

There is no “combo” except for the fact that the script is designed so that the indexes of the destinations list always match the indexes of the attractions list. The first expression for both add_attraction() and find_attractions() is get_destination_index(destination)

The whole thing could be much better handled with dictionaries, but they do not arise until the next course.

2 Likes

Thanks Patrickd314 helpful as usual. The below line seems to populate the variable attractions_for_destination with whatever is in the correct index within the attractions list.

attractions_for_destination = attractions[destination_index]

I’m wondering which part of the code actually stores the attraction into the attractions list itself, so that other functions could then call that data.

The next one:
attractions_for_destination.append(attraction)

But how?

This line adds whatever is passed as attraction into an empty list attractions_for_destination, which is a local variable inside add_attraction()

How does the same value gets into a global variable attractions?

  print(attractions_for_destination) 
>>  []
  print(attractions) 
>> [[], [], [], [], []]
  attractions_for_destination.append(attraction)
  print(attractions_for_destination)
>> [['Venice Beach', ['beach']]]
  print(attractions)
>> [[], [], [['Venice Beach', ['beach']]], [], []]

Hello @arbuznik, and welcome to the Codecademy Forums!

Let’s look at this example code that features a list of lists:

list_of_lists = [["zero"], ["one"], ["two"], ["three"], ["four"]]
print(list_of_lists)
list_at_index_three = list_of_lists[3]
print(list_at_index_three)
# Test for identity
print(list_at_index_three is list_of_lists[3]) # True
list_at_index_three.append("spam")
print(list_at_index_three)
print(list_of_lists[3])
print(list_of_lists)

Output:

[['zero'], ['one'], ['two'], ['three'], ['four']]
['three']
True
['three', 'spam']
['three', 'spam']
[['zero'], ['one'], ['two'], ['three', 'spam'], ['four']]

After this line has executed, list_at_index_three and list_of_lists[3] refer to the same list:

list_at_index_three = list_of_lists[3]

Therefore, if we modify the list referred to by list_at_index_three, we are also modifying the list referred to by list_of_lists[3]. Since list_of_lists[3] is a component of list_of_lists, we are also modifying list_of_lists.

Within The Boredless Tourist project, this statement assigns a reference to one of the lists contained by attractions to attractions_for_destination:

    attractions_for_destination = attractions[destination_index]

Therefore, if we modify attractions_for_destination, we are modifying a component of attractions. Any change to attractions_for_destination will also apply to attractions.

3 Likes

So in the case of list of lists, = operator does not create a new copy of list in variable attractions_for_destination, but instead makes a reference to the original list.

Now it makes sense, thank you.

3 Likes