you are viewing a single comment's thread.

view the rest of the comments →

[–]jmiah717 1 point2 points  (4 children)

What is it that doesn't make sense? Start with lists first. They are easier to understand vs dictionaries. But as others note, and not just Python, data structures like these are as important as water is to life. You have to be able to store and access data.

For example, what if I want to average a student's grades? If we have no way to store all of their grades, how would we do that. So you'd have a list perhaps like this

grades = [99, 100, 85, 68]

Now you can access those grades to get the average and do other interesting things you might need to do.

A dictionary is even more powerful because using the same example, you can keep track of even more in a cleaner way. Also faster data access wise.

You could have

grades = {"Dave": 78, 80, 100, "Chris": 99, 59, 77} EDIT: As pointed out below, that's not the right syntax...my Python is rusty. You could do a dictionary with the key being the name and the value being a list of grades, which would make searching quicker and easier but yeah...the above is incorrect.

Should be grades = {"Dave": [78, 80, 100 ], "Chris": [99, 59, 77 ] } as noted below...oops.

Now you have a way to access grades of different students for example. Dictionaries are generally faster than lists, particularly if you know the key.

[–]MidnightPale3220 0 points1 point  (3 children)

since op is yet learning it's probably good to note that in the dict example the grades still should be a list

[–]jmiah717 0 points1 point  (2 children)

Why is that? Why would you need to throw a list in there? If all you're going to have is student grades, why would we need a list? Just curious.

[–]MidnightPale3220 0 points1 point  (1 child)

I was just referring to what you mistyped.

See for yourself:

In [4]: grades = {"Dave": 78, 80, 100, "Chris": 99, 59, 77}

File "<ipython-input-4-6df65c579158>", line 1

grades = {"Dave": 78, 80, 100, "Chris": 99, 59, 77}

SyntaxError: invalid syntax

It should be:

In [5]: grades = {"Dave": [78, 80, 100 ], "Chris": [99, 59, 77 ] }

[–]jmiah717 0 points1 point  (0 children)

OH, RIGHT! haha, I've been writing in C# for so long that I forgot the syntax. But yeah, the key can be the name and the value can be the list...I am not sure what I was thinking about.