Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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