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

dicts

def string_factory(list_of_dicts, string):
  soda_pop = []
  i = 0
  for i in range(len(list_of_dicts)): 
    dict_list = list_of_dicts[i]
  soda_pop.append(string.format(**dict_list))
  return soda_pop

[MOD: Added ```python markdown formatting -cf]

Hi Thomas!

try:

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

You can wrap your code in three backticks on top and bottom, and specify the language to syntax highlight on top

ex:

'''python

coding happens here

'''

5 Answers

Tobias Mahnert
Tobias Mahnert
89,414 Points

Hello Thomas,

please see the Code below.

def string_factory(dicts, strings): #create function and place the arguments (dictionary and a string)
  string_array = []                           #declare an array variable which will store the for loop information. its declared to     prevent the app to crash
  for dic in dicts:                            #creating the for loop using the dicts argument and creating a index argument which goes through every item in the dictionary, in this case dic
    string_array.append(strings.format(**dic))  #declaring whats happening in every item of the loop. the information from the dictionary is filled in with the format funcition into the string and gets appended (added) to the string_array

  return string_array        #return the completly filled array 

Hi Thomas!

try:

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

You can wrap your code in three backticks on top and bottom, and specify the language to syntax highlight on top

ex:

'''python

coding happens here

'''

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Thomas, instead of posting some other solutions, let's look at your code.

It will pass if indent corrected:

def string_factory(list_of_dicts, string):
  soda_pop = []
  i = 0
  for i in range(len(list_of_dicts)): 
    dict_list = list_of_dicts[i]
    soda_pop.append(string.format(**dict_list))  # <-- indent fix to align with dict_list
  return soda_pop

the reason it didn't work is because I was using an int i instead of key which would only work for tupples

so

for key in list_of_dicts:
  soda_pop.append(string.format(**key))
return soda_pop
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Be that as it may, when I paste the indent-corrected solution I posted above, the challenge passes.

how come it says error tupple index out of range when string

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

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

You do not need the curly braces { } to define a string.

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

Using the curly braces defines a set

>>> string = {"Hi, I'm {} and I love to eat {}!"}
>>> print(type(string))
<class 'set'>

In the julia programming language it fixed this little nuance with a simple $ sign for string formatting.

You should teach Julia I love it!