Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,082 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... :-) ).