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
Jan M
1,053 PointsWhy does this Python dictionary code not work? (try to enter a variety of names from a dictionary into several strings)
I just tried this but it doesnt work, any ideas? I am trying to get python to pull the names out of the dictionary and then insert them into the string.
my_string="Hey! I am {name}"
my_dict={'name1':'Bob','name2':'Jan','name3':'Kenneth'}
print(my_string.format(**my_dict))
[MOD: removed extra space before ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,468 PointsFormatting issue caused by extra space before ```python
2 Answers
colinjames
12,576 PointsHi there, I was able to iterate through all of the names in the dict using the following code:
my_string = "Hey! I am {}"
my_dict={'name1':'Bob','name2':'Jan','name3':'Kenneth'}
for key, value in my_dict.items():
print(my_string.format(value))
# Hey! I am Jan
# Hey! I am Kenneth
# Hey! I am Bob
Hopefully this is helpful!
Jan M
1,053 PointsThanks alot for the answer. Would there also be a way to have several dictionaries containing the birthplaces, names, and ages (for example) of several people and then have a string filled with that information for each person?
colinjames
12,576 PointsDefinitely! You could accomplish this a number of different ways. One way would be to have a dict who's values are lists with an individuals information:
friends = {
"friend1": ["Axl Rose", "Los Angeles", 53],
"friend2": ["Prince Rogers Nelson", "Lake Minnetonka", 57]
}
Another (better) way would be to have a list of friends with each index being a dict which represents an individual. Like this...
friends = [
{
"name": "Axl Rose",
"birthplace": "Los Angeles",
"age": 53,
},
{
"name": "Prince Rogers Nelson",
"birthplace": "Lake Minnetonka",
"age": 57,
}
]
Then you just have to iterate through either of these data structures. Good luck!
Jan M
1,053 PointsI just tried this but for somereason it doesnt work, are there any mistakes in it?
friends = [{"name": "Axl Rose","birthplace": "Los Angeles","age": 53,},{"name": "Prince Rogers Nelson", "birthplace":"Lake Minnetonka","age": 57,}]
my_string="Hey! I am {name}, and I am {age} years old. I was born in {birthplace}."
for key, value in friends:
print(my_string.format(value))
Also is there a way that I would be able to only print one of the set of data, for example when I only want to look up one persons data could I enter their name an be returned all their data?
Jan M
1,053 PointsJan M
1,053 PointsThe " is actually a quotation mark (") but it shows messed up