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 trialThomas Katalenas
11,033 Pointsdicts
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]
5 Answers
Tobias Mahnert
89,414 PointsHello 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
Jackson Stokes
29,058 PointsHi 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
Treehouse Moderator 68,454 PointsThomas, 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
Chris Freeman
Treehouse Moderator 68,454 PointsWhy was this down voted?
Thomas Katalenas
11,033 Pointsthe 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
Treehouse Moderator 68,454 PointsBe that as it may, when I paste the indent-corrected solution I posted above, the challenge passes.
Thomas Katalenas
11,033 Pointshow come it says error tupple index out of range when string
string = {"Hi, I'm {} and I love to eat {}!"}
Chris Freeman
Treehouse Moderator 68,454 PointsYou 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'>
Thomas Katalenas
11,033 PointsIn the julia programming language it fixed this little nuance with a simple $ sign for string formatting.
You should teach Julia I love it!
Jackson Stokes
29,058 PointsJackson Stokes
29,058 PointsHi Thomas!
try:
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
'''