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

reza sajadian
reza sajadian
718 Points

Python dictionaries

Here is my quiz,

Create a function named string_factory that accepts a list of dictionaries and a string. Return a new list build by using .format() on the string, filled in by each of the dictionaries in the list.

and here is my answer.

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(): for items in dicts: new_list = string.format(**dicts) return(new_list)

can anybody help me finding the problem? Thanks.

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():
  for items in dicts:
    new_list = string.format(**dicts)
return(new_list)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

There were a few items to fix in your code:

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):  # <-- add string parameter
  new_list = []  # <-- initialize new_list
  for items in dicts:
    new_list.append(string.format(**items))  # <-- append built string using the "items" dict
  return new_list  # <-- indent 'return' to be inside of function definition
reza sajadian
reza sajadian
718 Points

Thank you so much Chris. It was totally helpful, and your comments are really guiding me through. I had the Append, and the function arguments missing. Best regards.

Stefan Vaziri
Stefan Vaziri
17,453 Points

Why did you use the append function? Wouldn't format swap the respective words in the right place in the sentence? Thanks Chris!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

I used the append because the return value is expected to be a listand it's the easiest way to add an item to the end of a list.

You are correct that using **items has the potential of producing the key-value pairs in an arbitrary order. This is resolved by using named fields in the format string. {name} and {food} will receive the correct value based on the key. When using named fields order of the format arguments doesn't matter:

>>> string = "{first} before {last}"
>>> string.format(first='1', last='2')
'1 before 2'
>>> string.format(last='5', first='3')
'3 before 5'

I think you deserved best on this one ;).

Hi there Reza. I have taken a look at your code and noticed that no arguments are required to be passed in by your function. The string_factory function takes 2 arguments; a list of dictionaries and a string. Here is my solution to the challenge:

def string_factory(dicts, string):
  new_list = []
  for i in dicts:  #for every dictionary in dicts
    new_list.append(string.format(**i)) #format the key and value into string and append it to new_list
  return new_list
reza sajadian
reza sajadian
718 Points

Thank you Haider. It was completely helpful. Best regards.