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

John Schut
John Schut
2,317 Points

strings.py solution

I made a solution which works in my workspace, but in the tool it's complaining about the update sequence being 1, while it should be 2. Beats 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(string, dicts):
  new_list_of_strings = []
  count = 0
  for item in dicts:
    dicts_sub = dict(dicts[count])
    new_list_of_strings.append(string.format(**dicts_sub))
    count += 1

  return(new_list_of_strings)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Your parameter order matters. The task says "Create a function named string_factory that accepts a list of dictionaries and a string" in that order.

Your code for iterating the list of dicts is unusual.

  for item in dicts:
    dicts_sub = dict(dicts[count])

The for-loop assigns to item each dict listed in dicts and it can be used directly in place of dicts_sub

  for item in dicts:
      new_list_of_strings.append(string.format(**item))
John Schut
John Schut
2,317 Points

Hi Chris, thanks! I introduced the dicts_sub, because item directly seemed not to work eighter (but probably I did somwthing else wrong... :-) ).