The Boredless Tourist Challenge Question

Hello :slight_smile: First time Poster here…

I am working on The Boredless Tourist Project currently and am trying to wrap my head around one aspect of the last function (shown below) I was working on.

I am having trouble understanding why defining the new variable “attractions_for_destination” to the appended “attractions” list would then update the list outside the scope of the function versus just creating a new list called “attractions_for_destination” and returning that.

I do see that it works, just don’t understand the logic.

Appreciate any insight to help this click for me!


def add_attraction(destination, attraction):

  try:

    destination_index = get_destination_index(destination)

    attractions_for_destination = attractions[destination_index].append(attraction)

  except SyntaxError:

    return

That line attractions_for_destination = attractions[destination_index].append(attraction) is probably not going to do what you want. The .append method modifies a list in-place. The method itself will return the None object which is probably not what you want. You’d generally use .append in a line on its own e.g. lst.append(5) because it simply mutates the underlying list object, it doesn’t have a useful return like some other methods do.

I’m guessing that’s not exactly what you’re after though so if you could clear that up and then explain what’s troubling you that might be helpful, ta.

Thank you for the attempt and apologies if I am not explaining well.

The function is from the project video supplied when you click the “Get Unstuck” link. I also verified that it produced the expected results. I had to click there after my own attempts were not so successful at returning the appended attraction list.

I am just trying to understand why we create a previously non-existent label of *attractions_for_destination in order to append and return the attractions list.

Honestly, I may just need to sleep on it and look at it again tomorrow :nerd_face:

Here is the full project code including the test print() call (Should have included it the first time!):

destinations = ['Paris, France', 'Shanghai, China', 'Los Angeles, USA', 'SĂŁo Paulo, Brazil', 'Cairo, Egypt']

test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]

attractions = [[] for destination in destinations]

def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = get_destination_index(traveler_destination)
  return traveler_destination_index

print(get_traveler_location(test_traveler))

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

add_attraction('Los Angeles, USA', ['Venice Beach', ['beach']])
print(attractions)

Hmm, it’s a redundant name since it’s never even used again. At the end of the day that label does nothing, the line would simply be-

attractions[destination_index].append(attraction)

Note that this function doesn’t actually return anything. The name attractions doesn’t exist in the function local scope but can be accessed from the global scope. So when that name is used you’re actually affecting the original attractions list, the one defined near the top → attractions = [[] for destination in destinations]. Since the list is a mutable type you can modify it with this reference even in the function local scope.

Whether or not that’s a good use of a function is a debate for another day :grinning_face_with_smiling_eyes:.

1 Like

Really appreciate the explanation on scope. Thank you!

I was able to update the function to the code above and it still worked. :tada: And it looks so much better without the redundancy!

1 Like