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 Dictionary Iteration

V K
V K
5,237 Points

How normal is it to be completely lost on the challenges?

I have watched the videos and have taken meticulous notes. I've done fine on the quizzes and I'm able to follow most of what I've seen in the videos. However, I am lost to the point of not even having a clue where to start on the challenges. The most recent challenge asked "Create a function named string_factory that accepts a list of dictionaries and a string. Return a new list built by using .format() on the string, filled in by each of the dictionaries in the list."

When looking at the solution, it makes little to no sense when compared to the stuff that's being presented in the videos.

When I look at the solution..it sorta makes sense, but there's no way I would have come up with the solution on my own.

I've completed one python course here so far. But I'm feeling extremely discouraged by my inability to grasp material presented in a beginner course.

Nick Osborne
Nick Osborne
4,612 Points

Hey Vincent, normal I think! I agree that the challenges in the Python course are much more challenging than those in other beginner courses that I've worked on (like html and css). I too, had to often look at solutions in order to figure out what was going on; often printing them out and deliberating over them so I understood before moving on. But keep going! I found that it was well worth the effort in the end!

4 Answers

fahad lashari
fahad lashari
7,693 Points

Hi, I can totally relate to your situation. I over came this by following the instructions step by step as I read through them. For example you mentioned: "Create a function named string_factory that accepts a list of dictionaries and a string. Return a new list built by using .format() on the string, filled in by each of the dictionaries in the list."

This question is from the code challenge. In the code challenge some additional information has been given to you already. Let's go ahead and add it here:

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}!"

We have a list by the name 'dicts'. The square brackets '[]' indicate that it is a list (refer to python basics). And we a string variable. The "" indicates that it is a string (refer to python basics).

Now let's continue to break this problem down

I would firstly look at the first segment of this instruction: "Create a function name string_factory" and I would go ahead and do just that:

def string_factory():

Then I would look at: "that accepts a list of dictionaries and a string". By 'accepts' they mean 'takes the argument' and by 'argument' it means the parameter and by 'parameter' it means the value between the parenthesis(brackets) which are in the defining of the function as you can see so, I would go ahead a do:

def string_factory(dicts, string):

Then I would look at: "Return a new list" and I would go ahead and define a variable named 'list' as I know they just asked me to return a 'new' list. This mean I have to create a list myself, indicating that I need to define a variable for creating a new list and then writing a return function to return it(get back):

def string_factory(dicts, string):
      list = []
return list[]

notice how I didn't add any functionality yet as I have not read the part of the instruction "built by using .format() on the string, filled in by each of the dictionaries in the list."

Now I will read the next part: "built by using .format() on the string". I would go ahead and do:

def string_factory(dicts, string):
      list = []
      string.format()
return list[]

It is pretty easy to understand what "built by using .format() on the string" would mean. "using .format()" on the string. In other words apply ".format()" function to the string that is in the argument(in the parenthesis of the function) of the function string_factory. There is no other keyword named "string" so it obviously is talking about the string that we put in the argument earlier.

I will now go ahead and read the next part: "filled in by each of the dictionaries in the list." Now let's concentrate on each of the key words in this segment and try to figure out what they can mean. Think about what 'filled' could mean, if you have done the python basics course, you know that to 'fill' a 'list' you have a few tricks such as using the 'extend' function or using the 'append' function.

Now let's look at the key word 'each', this means a single item and one at a time and not all together. We know (from python basics course) that the most appropriate method of filling a list one item at a time is 'append' (refer to the python basics course).

Now let's look at the keyword 'dictionaries' which represents each of the items that are syntaxed like a dictionary in the list 'dicts'. This is means the list 'dicts' is made up of multiple dictionaries.

Now let's look at the keyword 'list'. In the python basics course we learnt how to 'iterate' (refer to python basics) through a list or 'loop' through a list. This is done using a method called 'for loops'. Which goes through the list one item at a time and then we can do something with each of the items.

So let's let complete this function. The question is basically saying that use the list of dictionaries and the string I have given you and give me back as many of the strings as there are dictionaries and fill in the missing gaps in those strings with the items in the dictionaries. Let's do that simply:

def string_factory(dicts, string):

    list = []
    for dictionary in dicts:                                   #This is the for loop to go through the list
        list.append(string.format(**dictionary))  #we are using append to add the 'dictionary' items to the string that is                         
    return list                                                       #current being filled

#we are then returning it using the 'return' function.

I hope this has made things clearer for you the next time you are stuck. Just try to break the question down into small segments and follow one step at a time. Once you understand each piece then you can put the pieces together slowly and make the programme work.

Kind regards,

Fahad

Please vote best answer if this helped you :)

Yep. It's a 100% normal. I used to be like that all the time.

I kinda agree, though. The Python course is kinda harsh (it's code challenges are super tricky for beginners and the videos can also be confusing)

"However, I am lost to the point of not even having a clue where to start on the challenges."

I couldn't have said that better myself. I have no real experience coding before starting this course, so I've been feeling the same way, and I really needed to hear this from somebody else. I've been wondering if I am missing something in the videos, or just not grasping it at all. It's been discouraging for the previous few challenges.

Like Nick, I've also been printing out the challenges and solutions to keep track of learned concepts.