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! While you're at it, check out some resources Treehouse students have shared here.

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

String_Factory Confused [Python]

Can someone share some light to me as to what I am doing wrong. This works on REPL, but I get the error "list" object has no attribute "format." on Treehouse. Which I assumed I would get the same reply on REPL, but I didn't. Any reason why?

strings.py
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(stringa, listOfDicts):
    my_list= []
    for dic in listOfDicts:
        my_list.append(stringa.format(**dic))
    return(my_list)
string_factory(string, dicts)

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

You solution is actually a good one, with a minor issue though. Pay attention to this line in the challenge description

Create a function named string_factory that accepts a list of dictionaries and a string.

It's clear that string_factory function takes 2 arguments: 1st a list of dictionaries; 2nd a string. The 2 arguments in your code wasn't in the correct order, that's where the problem is. Fix it and your code would pass.

def string_factory(listOfDicts, stringa):  # <= swap the 2 arguments.
    my_list= []
    for dic in listOfDicts:
        my_list.append(stringa.format(**dic))
    return(my_list)

Hope it helps, happy coding :)

jason chan
jason chan
31,009 Points
def string_factory(dicts,string):
  result_list = []
  for item in range(len(dicts)):
    result_list.append(string.format(**dicts[item]))
  return result_list

create a function pass in two arguments, create a variable to hold array. loop through the items in dictionary. then loop through the item through iterpolation