'Syntax error Python Flask iterating through a dictionary from an imported file and outputting html

Working on a codecademy project for the Python Flask course. I'm a bit rusty and I haven't combined python and html before. I'm trying to iterate through a dictionary located in helper.py called pets. This is the library:

pets = {
    'dogs': [
        {
            'name': 'Spot',
            'age': 2,
            'breed': 'Dalmatian',
            'description': 'Spot is an energetic puppy who seeks fun and adventure!',
            'url': 'https://content.codecademy.com/programs/flask/introduction-to-flask/dog-spot.jpeg'
        },
        {
            'name': 'Shadow',
            'age': 4,
            'breed': 'Border Collie',
            'description': 'Eager and curious, Shadow enjoys company and can always be found tagging along at your heels!',
            'url': 'https://content.codecademy.com/programs/flask/introduction-to-flask/dog-shadow.jpeg'
        }
    ],
    'cats': [
        {
            'name': 'Snowflake',
            'age': 1,
            'breed': 'Tabby',
            'description': 'Snowflake is a playful kitten who loves roaming the house and exploring.',
            'url': 'https://content.codecademy.com/programs/flask/introduction-to-flask/cat-snowflake.jpeg'
        }
    ],
    'rabbits': [
        {
            'name': 'Easter',
            'age': 4,
            'breed': 'Mini Rex',
            'description': 'Easter is a sweet, gentle rabbit who likes spending most of the day sleeping.',
            'url': 'https://content.codecademy.com/programs/flask/introduction-to-flask/rabbit-easter.jpeg'
        }
    ]
}

Here is my app with flask which for now is just running at http://localhost:5000/:

from flask import Flask
from helper import pets
app = Flask(__name__)

@app.route('/')
def index():
  return '''
  <h1>Adopt a Pet!</h1>
  <p>Browse through the links to find your new furry friend:</p>
  <ul>
    <li><a href ="/animals/dogs">Dogs</a></li>
    <li><a href="/animals/cats">Cats</a></li>
    <li><a href="/animals/rabbits">Rabbits</a></li>
  </ul>
  '''

@app.route('/animals/<string:pet_type>')
def animals(pet_type):
  html = f'''
  <h1>List of {pet_type}</h1>
  '''
  for key in pet_type
  html += '''
  <ul>
    <li>{name[key]}</li>
  </ul>
  '''
    
  return html

The problem is in the last chunk. I want to print the names of all the pets from the indicated pet_type (which for now can be indicated by changing the URL or clicking the link on the homepage), but I keep getting a syntax error on this line:

for key in pet_type

What is the correct syntax?

--

Alright, I corrected the lack of a colon and indent, and updated the code to also iterate through the actual dictionary I wanted instead of a string:

@app.route('/animals/<string:pet_type>')
def animals(pet_type):
  html = '''
  <h1>List of {pet_type}</h1>
  '''
  for key in pets[pet_type]:
    html += f'''
    <ul>
      <li>{pet_type['name']}</li>
    </ul>
    '''

Updated:

@app.route('/animals/<string:pet_type>')
def animals(pet_type):
  html = f'''
  <h1>List of {pet_type}</h1>
  '''
  for key in pets[pet_type]:
    html += f'''
    <ul>
      <li>{pets[pet_type][key]['name']}</li>
    </ul>
    '''
    
  return html

I'm getting decimated by this:

TypeError: list indices must be integers or slices, not dict

But I need the dictionary value.

--

Okay it's done. I needed to change

for key in pet_type:

to

for key in range(len(pets[pet_type])):


Solution 1:[1]

Think you need to indent your code and add colon

  for key in pet_type:
    html += '''
    <ul>
      <li>{name[key]}</li>
    </ul>
  '''
    
  return html

That being said you are also trying to iterate through pet_type which is a string, not an iterable (a list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1