Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries String Formatting with Dictionaries

'list' object has no attribute 'items'

I have looked at so many answers to this task challenge on forums and I still can't make it work. Can someone just post a picture of the correct answer? Thanks!

dicts = [
    {'name': 'Michelangelo',
     'food': 'PIZZA'},
    {'name': 'Garfield',
     'food': 'lasanga'},
    {'name': 'Walter',
     'food': 'pancakes'},
    {'name': 'Galactus',
     'food': 'worlds'}

string = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, string):
  string = []
  for k, v in dicts.items():
    return (string.format(**dicts))
print (string_factory(dicts, string))

1 Answer

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, dicts is a list and lists don't have an .items() method. You need to loop through dicts and then you can get the key and value from each item in dicts.

def string_factory(dicts, string):
    strings = []
    for d in dicts:
        for k, v in d.items():
            # Do something here
    return strings