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

Anup Dudani
PLUS
Anup Dudani
Courses Plus Student 939 Points

string_factory function

my_dicts = [ {'name': 'Michelangelo', 'food': 'PIZZA'}, {'name': 'Garfield', 'food': 'lasanga'}, {'name': 'Walter', 'food': 'pancakes'}, {'name': 'Galactus', 'food': 'worlds'} ]

my_string = "Hi, I'm {} and I love to eat {}!"

def string_factory(my_dicts, my_string): string_list=[] for ele in my_dicts: formated_string=my_string.format(ele['name'], ele['food'])

string_list.append(formated_string)

return string_list

print(string_factory(my_dicts, my_string))

I am get the desired out put that is a list of formatted strings but I am not able to submit the answer correctly, can someone help me?

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(my_dicts, my_string): 
  string_list=[]
  for ele in my_dicts:
    formated_string=my_string.format(ele['name'], ele['food'])
    string_list.append(formated_string)
  return string_list

You're very close but remember the specific lesson was unpacking the dictionary. Hint: Use **

1 Answer

you need to unpack the dictionaries so that the keys and values are passed onto the string. from what I see you chose to name your dictionaries ele but you could use dictionary. To unpack the dictionary we use **dictionary which in your case will be **ele.

def string_factory(my_dicts, my_string):

string_list = []

for ele in my_dicts:

    formated_string = my_string.format(**ele)

    string_list.append(formated_string)

return string_list