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

helmi al kindy
helmi al kindy
1,371 Points

How do I map instead of listing ?

I believe that the for keyword is used for mapping but I don't know what I'm doing wrong.

Thanks to all who give helpful answers

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(dict_list, string):
  for food in dicts:
    print(string.format(**dicts))
Brandon Mowat
Brandon Mowat
8,186 Points

first off, dicts is a list. The elements inside the dicts list are dictionaries.

second, you should have a return statement that returns a new list of strings.

I suggest re-watching the video for reference.

Hope that helps!

3 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points

That is because treehouse's interactive code environment is passing dicts, string as function arguments when it calls your function. so if you change the function defn to def string_factory(dicts, string) you will pass the challenge.

Anish Walawalkar
Anish Walawalkar
8,534 Points

You did everything correct except for 2 tiny things:

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(dict_list, string):
  for food in dicts:
    return string.format(**food)

you did:

print(string.format(**dicts))

unfortunately dicts is a list of dictionaries where as food is a dictionary in the list dicts, your for loop should do this:

for food in dicts:
    return string.format(**food)

ALSO you need to return not print. that should do the trick.

hope this helped

helmi al kindy
helmi al kindy
1,371 Points

I tried what you said but it's not working.

Anish Walawalkar
Anish Walawalkar
8,534 Points

Sorry, I was wrong the first time I answered your question. A few thing that I want to clarify:

  1. Since you are creating the variables "dict" and "string" outside the function "string_factory", these variables (dict and string) are available in the scope of your function hence you do not need to pass them as arguements
  2. Your function should return a list

I solved the problem in pythons interactive shell. This is how your function should look:

def string_factory():
    x = []
    for food in dicts:
        x.append(string.format(**food))
    return x
helmi al kindy
helmi al kindy
1,371 Points

I copied and pasted what you typed but I'm getting another weird error; Bummer! string_factory() takes 0 positional arguments but 2 were given When there are no arguements of any kind.

But if I try it on my local pc it works fine.