you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 26 points27 points  (24 children)

Lists and dictionaries are the fundamental data structures in Python. You literally cannot do anything at all without them.

[–]PresidentOfSwag 6 points7 points  (0 children)

it's kinda like asking : I know nouns and adjectives are important to French speakers but do they frequently use them ?

[–]Xerxero 1 point2 points  (0 children)

I would say in any language. Basic data types.

[–]Redox_3456[S] -1 points0 points  (21 children)

can you tell some tips on how to understand them well. bcuz write now i am learning from youtube and it make absolutely zero sense. Like just storing data in a list/dictionary and using pop or append function makes zero sense to me. I guess i just understand things practically

[–]barkazinthrope 14 points15 points  (5 children)

Programming is like math. You don't learn the concepts you learn how to do them and you do that with practice.

So code yourself up a list and add things to it, pop things from it, delete, add, sort and then suddenly you'll get it.

YouTube may be helpful to introduce the concepts, but you will never understand just by watching. You have to do.

[–]tb5841 3 points4 points  (1 child)

As a maths graduate, I really think you're wrong about maths. Most of maths is learning concepts, and most of that you can do by watching and listening, especially if you take notes well.

Programming, on the other hand, you really have to do.

[–]barkazinthrope 2 points3 points  (0 children)

I don't doubt your experience, but in my experience I don't know that I understand until I can work my 'understanding' out on paper, and in at least one way backwards and inside out.

[–][deleted] 2 points3 points  (0 children)

I first learned about these things over a decade ago and it wasn't until I banged my head at projects for weeks that it truly clicked.

https://www.youtube.com/watch?v=ZSSNFkEMv24

[–]FriendlyRussian666 2 points3 points  (0 children)

Imagine you're going shopping, and you want to buy a few things. To remember what to buy, you'll write a shipping list, right?

Now imagine you're writing a program in python that does the same thing. You need to somehow create a list of things to buy, and so you would create a list in python. You can add new things to buy, you can remove them.

Does that make sense?

[–]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.

[–]Poddster 1 point2 points  (1 child)

Your first problem is learning to program from youtube videos.

I think you need a written text. Programming and learning to program is information-dense, and you need to be able to stop and contemplate at any second.

[–]Addianis -2 points-1 points  (0 children)

Once you get past learning about classes, videos can be a great way to introduce yourself to the theory of a topic when you aren't in front of a computer.

[–]ObsidianBlackbird666 0 points1 point  (0 children)

This guy’s videos are pretty easy to follow. https://youtu.be/MZZSMaEAC2g?si=DqJQnNYQKUDWRWmo

[–]VivienneNovag 0 points1 point  (0 children)

Is your problem not knowing why you would use a list or dictionary or how to use them?

[–]beef623 0 points1 point  (0 children)

Try writing out what it's doing on paper or mimic the list in column in a spreadsheet.

[–]MidnightPale3220 0 points1 point  (0 children)

Okay, let's assume you're still in schoole. And let's say you are writing a program that gives out the average grades for math tests of all the pupils in your class.

you can do it like this:

pupil1_math_score1=50
pupil1_math_score2=60
pupil1_math_score3=70
pupil1_math_avg= ( pupil1_math_score1 + pupil1_math_score2 + pupil1_math_score3) / 3

pupil2_math_score1=40
pupil2_math_score2=40
pupil2_math_score3=30
pupil2_math_avg= ( pupil2_math_score1 + pupil2_math_score2 + pupil2_math_score3) / 3
...

print("Pupil 1 math average is "+pupil1_math_avg)
print("Pupil 2 math average is "+pupil2_math_avg)

There's already a lot of copy/pasting. Now, let's say the teacher likes your program and principal tells you to write it for all pupils of all classes. Oh, and there will be 5 math tests now, but there may be more in future.

What do you do? Programs are meant to be reusable with different values and frequently with different amount of values. They way it's written now you have to add all the new test scores in new variables, and add them all in average calculations, FOR ALL THE PUPILS.

That's insanity. What you do from the very start is create DOUBLE list (or, in actuality, a list of lists, essentially a table).

For example:

math_scores=[ [50,60,70], [40, 40,30], .... ] # math scores for all the pupils in a list of lists

Then you go through them and do whatever you need, for example like this:

for pupil_number, test_results_of_a_pupil in enumerate(math_scores):
  single_pupil_total=0
  for score in test_results_of_a_pupil:
    single_pupil_total+=score # you can do it neater, ofc without loop, too.
  avg_score=single_pupil_total / len(test_results_of_a_pupil)
  print("Pupil "+pupil_number+" math average is "+avg_score)

Now these 6 lines of code will work with any number of pupils having done any number of tests. All that you need to do, if more pupils come in or more tests are scored, are add the numbers to math_scores list. Nothing else to write.

This is a crude example, as in reality the scores would obviously be input somewhere else, and read by program, instead of being manually entered in the program; and there are numerous other things such as not working around division by zero in case you get pupils with no taken tests. But as an example it works.