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 trialAndrew Forte
1,296 PointsChallenge: string.py - String Factory
Hi Treehouse,
Seems like I have found my kryptonite with the course. I understand the principles of the lists and dictionaries but when it comes to writing the code logic I am useless :)
I am stumped on this challenge and this is as far as I got. My apologies as I know that this is a poor attempt but I am not sure what to do next. I know that there is some form of a for loop that comes into play that iterates through the dicts list of dictionaries and add's the values to my new list using the string.
Anyone's help would be much appreciated!
Andrew
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}!"
# creating a function named string_factory that accepts dicts and strings above
def string_factory(dicts, string):
#creating a new list called new_list
new_list = []
1 Answer
Fenimore Cooper
3,591 PointsHi Andrew,
The question asks that you convert data from a dictionary format into a list -- which isn't too hard -- but dictionaries can be tricky to get the hang of at first. If I were you I'd mess around with dictionaries on your own computer -- I find its a lot easier to debug problems when you can see what your program is returning -- which you can't on the 'work space'.
Iterating over dictionaries is similar to iterating over lists -- except each iteration is a new dictionary, rather than a new list item. In our example, this is our first tiny dictionary: {'name': 'Michelangelo', 'food': 'PIZZA'}. So, if you do a for loop over our list of dictionaries -- the name of the key will act as, well... a key to the stored value. In the case of the code below, entry['name'] gets you each name i.e. 'Michelangelo', and entry['food'] gets each food item i.e. 'PIZZA.' The rest is just a sting substitution &c.
Hope that helps.
def string_factory(dicts, string):
holding_list = []
for entry in dicts:
holding_list.append(string.format(name=entry['name'], food=entry['food']))
return holding_list
Andrew Forte
1,296 PointsAndrew Forte
1,296 PointsHi Fenimore,
Thank you for this, its great! Much appreciated for helping me.
I am certainly going to take your advice and mess around with both lists and dictionaries on my computer. As you say much easier to debug vs using the workspace. I think I just need to play more until the penny drops :)
Thanks again, I really appreciate it!
Thanks,
Andrew
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsThere is a shortcut available when you want to turn a dict into a list of keyword arguments.
If in the first iteration,
entry
were{'name': 'Michelangelo', 'food': 'PIZZA'}
, then**entry
is equivalent toname='Michelangelo', food='PIZZA'
This means the loop statement can be written as:
holding_list.append(string.format(**entry))