In Boredless Tourist 41, how do I get the attraction name alone?

The link to an exercise - https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-boredless-tourist/projects/the-boredless-tourist

On step 41, I need to get the attraction name out of the attractions in city, but I get a weird output:
[‘V’, ‘beach’, ‘L’, ‘art’]

My code is:

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

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

# Get destination index from the list of destinations (i.e. Paris, France would return 0)

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

# Get traveler's destination index from the list of destinations by matching traveler's destination to the destinations list (i.e. test traveler's Shanghai, China would return index 1 from destinations list )

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

test_destination_index = get_traveler_location(test_traveler)

print(test_destination_index)

attractions = []

# We create a list of lists. For every name in destinations we add an empty list to attractions

for item in destinations:
	count = 0
	if destinations.index(item) < len(destinations):
	 attractions.append([])
	 count +=1

print(attractions)

# We add an attraction to attractions list to the position relative to destinations list

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

add_attraction('Los Angeles, USA', ['Venice, Beach', ['beach']])
add_attraction("Paris, France", ["the Louvre", ["art", "museum"]])
add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historical site"]])
add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])
add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add_attraction("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add_attraction("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])

print(attractions)

def find_attraction(destination):
	destination_index = get_destination_index(destination)
	attractions_in_city = attractions[destination_index]
	attractoins_with_interest = []
	possible_attraction = []
	for attraction in attractions_in_city:
		for item in attraction:
			possible_attraction.append(item[0])
	return possible_attraction

print(find_attraction('Los Angeles, USA'))

Please help me get this straight or what’s my mistake? How am I receiving [‘V’, ‘beach’, ‘L’, ‘art’] output from my code?

1 Like

Meant step #42, apologies

I need to have an output [‘Venice, Beach’, ‘LACMA’]

Do you really need the second nested for loop? i.e.
Do you really need the for item in attraction: loop?

With the argument (Los Angeles) chosen in your code, attractions_in_city will be:

[['Venice, Beach', ['beach']], ['LACMA', ['art', 'museum']]]

The first loop for attraction in attractions_in_city is sufficient for you to access the first element of each attraction.
How? → attraction[0] will help you target the desired element.

In the current form, the second for loop iterates over each entry in an attraction and appends item[0]. For the first attraction, the first item will be a string and item[0] of that string is the first character “V”. The second item is a list, so item[0] will be the first element of that list i.e. the string “beach”. Similarly, for the second attraction, the two iterations of the second for loop will append “L” and “art”. That is why you are seeing the output mentioned by you.
BTW, you also seem to have a stray line attractoins_with_interest = []

1 Like

This is it, thank you very much for the detailed response

1 Like