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

Here's the repost cause it's still giving me the same error

same mapping error

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(dicts, string):
  strings = {}
  for item in dicts:
    string.format(**dicts)
  return strings

print(string_factory(dicts, string))

2 Answers

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

Hi, Carina De Jager , there're issues with the code in 2 spots.

def string_factory(dicts, string):
  strings = {}             # spot 1
  for item in dicts:
    string.format(**dicts)    # spot 2
  return strings
  • In spot 1, the strings variable needs to be a list (not dictionary) to hold the the formatting results.
  • In spot 2, while looping through the dictionary, string.format(), the argument to this function call should be **item, not **dicts; also, you need to append the result to the strings list as well.

The corrected version of the code should be sth like this.

def string_factory(dicts, string):
  strings = []
  for item in dicts:
    strings.append(string.format(**item))
  return strings

Hope it helps, let me know if there's further question.

How do you get a print result when running this fucntion? This works but if you run it nothing happens. How can add to this function to get a result printed or something like that when it runs?

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!

Yes it did! Thank you very much. The explanation was very helpful as to why I needed to do certain things. My brain seemed to be having trouble processing why you need to append the item to the strings variable if there isn't anything in the variable ( I didn't consider it to move one item into an empty list, I was thinking in terms of 'append' as sending something to the end of the list.). Is this the most commonly used way for sending things into blank lists? Just curious.

William Li
William Li
Courses Plus Student 26,868 Points

Hi, Carina, you need to append the result of each format() to strings variable because, well, that's what the question is asking for.

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

If we take a look at the original code template provided by the challenge

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}!"

The dicts variable here is a list, each item on this list is a dictionary.

The challenge description is basically asking you to loop through each item on the dicts list, and pass the item as argument to string.format() call, and then append each of the formatting result to a new list, which is the strings variable in your code.