Ok! Had a nice refreshing break, yesterday being Sunday.
Back to work today!
If today’s notes, feel a little light, that’s because I was struggling with exercises.
- A lightweight solution to classes, if I am just combining all sorts of data structures is the built-in
collections. Some of them could be
- List of Lists
- List of Tuples
- List of Dictionaries
- Dictionary of Dictionaries
- Dictionary of Lists
- Mostly use them to solve tricky issues. If I find myself using lots of functions, just to deal with these structures, I am better off using classes.
- These structures also lend themselves to being converted into classes.
- Sometimes it’s just a matter of adding a few methods, et voilà, I have a class!
Lists of lists
- As simple to create as
l = [[1,3,2],[4,5,6],[7,8,9]]
- If I do a
l, I’ll get the first element, the list
- To get at the elements inside that list, I can go
lwhich will give me the number, 3.
- Behind the scenes,
- If I do a
- We can either work with them with indices like up above, or iterate over them, with for loops or comprehensions
- Better to use lists of tuples, if I am dealing with mixed types, specially those that are not apt to change, like records of some sort. Something like a list of details about movies.
Learning / Feedback/ Experiences from doing exercises
- Learning to think atomically. Learning to break down each exercise into pieces and then learning to organise them in a manner, that I can solve it, step by step.
- What hints do Reuven’s results show?
- What should I compare first? What will be the aftermath? and so on.
- Gave up an exercise after two hours of trying.
- After peeking at the solution I realised that it was not because of something I forgot or did not practice, but once again, because of something I did not know.
- in this case that there are set operators to use between two iterator objects.instead of crazily stepping through everything like I did below, I could’ve just created a union list run through them.
- Just goes to show, there is lots for me to learn. And that there is a lot of tacit knowledge out there. And this I will only be able to figure out by writing lots of code!
- Also (note to self), time your self and be ok with giving up and looking at the solution. I seem to keep thinking that I am wrong or am missing something. Never the fact, that it might be something that I do not know.
- I am now strictly going to give myself 30 minutes per exercise and then move on.
- Lots of mistakes as usual.
- Wrong indentation, giving weird results.
Read all about my Python advanced data structures journey here