The Boredless Tourist

Hello, I was reading the code solution for the project “The Boredless Tourist” and I found a line of code that I don’t understand. What’s the point of having/creating a variable " attractions_for_destination". Why don’t we just write attractions[destination_index].append(attraction). Why do we have to assign it to a variable? Thank you.

 attractions = []
for destination in destinations:
def add_attraction(destination, attraction):
    destination_index = get_destination_index(destination)
    attractions_for_destination = attractions[destination_index].append(attraction)
  except SyntaxError:

Since multiple things are being done in a single statement, so the variable attractions_for_destination is not needed.

destination_index = get_destination_index(destination) # Get index of correct sublist.
attractions[destination_index].append(attraction) # Get correct sublist and append to it.

is sufficient and the variable attractions_for_destination isn’t needed.

If, you were accomplishing the above in a couple of steps (as opposed to a single step), then the variable would be useful.

destination_index = get_destination_index(destination)   # Get index of correct sublist.
attractions_for_destination = attractions[destination_index] # Get correct sublist.
attractions_for_destination.append(attraction)  # Append to the sublist.

In what situation would the variable “attractions_for_destination” is needed?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.