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

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,867 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,008 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