Learning Python, Day 9 — Examples of Lists
So last week, I learned about all the various types of collections there are, and I fully expect the ones that we're getting into now to break my brain just a little. But I have my various film directors, I have Google Colab open, I am ready to go.
08 — Multidimensional List Examples
And right off the bat, I get thrown by the simple 0. Trying to work out why my multidimensional list isn't behaving and finding out it's entirely just because I'm calling the second list 2 instead of 1 because I forgot that 0 starts the count.
Oh well. At least I still get to make fun of Brett Ratner, once I remember that.
directors = [["John Waters",
"David Lynch",
"David Cronenberg",
"Brandon Cronenberg",
"Ryan Coogler",
"Guillermo del Toro",
"Rose Glass",
"Panos Cosmatos"],
["Brett Ratner",
"Roland Emmerich",
"Chris Columbus",
"Paul W.S. Anderson"]]
Melania = directors [1][0]
print(Melania)
>> Brett Ratner
Also, I'm kind of annoyed that he just went "We're going to make these gigantic lists within a list" rather than "We're going to make a bunch of lists, and then call each list within our bigger list to make our life easier."
Because this is so easier to read:
hip_directors = ["John Waters",
"David Lynch",
"David Cronenberg",
"Brandon Cronenberg",
"Ryan Coogler",
"Guillermo del Toro",
"Rose Glass",
"Panos Cosmatos"]
bad_directors = ["Brett Ratner",
"Roland Emmerich",
"Chris Columbus",
"Paul W.S. Anderson"]
directors = [hip_directors, bad_directors]
Melania = directors [1][0]
Resident_Evil = directors[1][3]
St_Maud = directors[0][6]
print(Melania + ", " + Resident_Evil + ", " + St_Maud)
>> Brett Ratner, Paul W.S. Anderson, Rose Glass
And amuses me.
09 — Tuples
Calling it a "tuple" just makes me think of "tulpa" and then we are in my hipdirectors list because what was Twin Peaks: The Return but nothing but tulpas.
But no, this is about read-only values. Right.
And it's pretty straight-forward, I think. Although he was like "it only use them to temporarily store data", I could, instead, see it as, like, the official canon, the details that are set in stone and that you can't adjust. Like, conversion rates for pounds to kilograms or Fahrenheit to Celsius. That sort of thing. If you're building a weather app, the conversion formulae aren't going to change, so you stick them in the tulpe and then when you want to call the fancy math for converting, you just use, like, convert[0] and it'll pull up the right conversion formula.
10 — Dictionaries Examples
The idea of a key-item and a term-item have been embedded in my brain since the first time I heard of <dl>, <dt>, and <dd>. I love definition lists, and now I have a whole new way to categorise things? Hell yeah.
I'm testing it by making an inventory for Ice Drinks. So I can remember which ones I have. Yes, I am out of Mango Ice. I should get some more.
Ices = {
"Electric Ice":4
"Ice Blue":5
"Mojito Ice":4
"Mango Ice":0
}
print(Ices["Electric Ice"])
print(Ices["Mango Ice"])
>> 4
>> 0
11 — Ranges Examples
Ranges are for consecutive whole numbers, and have start and end values, as well as steps within.
And, yep, I really need to remember that 0 rule. Again. And that if I want to see each number, I have to convert it to a list.
I don't like how he's saying that if I want to see the even numbers in a list, I have to start at 0 instead of 1 and then step by 2. That's not how that works. There isn't a Star Trek 0.
treks = range(1,7)
good_treks = range(0,7,2)
print(list(good_treks))
>> [0, 2, 4, 6]
Day 9 — Results
- To make multi-dimensional lists, it's
listname = [ [item, item, item] [item, item, item]] - You can then call any individual item from that giant list of lists by using
listname[listnumber][itemnumber] - Don't forget everything starts with
0. Dangit. - You can count the number of times an item appears in a list by using
listname.count(item). - You can also count where in the index the item first appears by using
listname.index(item). - You can remove everything from a list with
listname.clear() - Tulpes are read-only lists. You can't add things to them, but you can call items from them.
- Tulpes use
()instead of[] - They are not tulpas. Stop.
- Dictionaries are key/term data lists. You can add in all sorts of things that connect to each other.
- Dictionaries use
{}instead of[]and(). - Dictionaries go
listname = { [itemkey]:[itemterm], [itemkey]:[itemterm] }. - You can add things to dictionaries by just doing
listname[itemkey] = itemterm. - You can remove items (both key and term) from dictionaries by doing
listname.pop(itemkey). - Ranges are only for whole numbers.
- You indicate a range by
name = range(start,end,step). (Step is optional). - You can reverse a range by doing
name = reversed(rangename) - You can also do
inandnot into see if your range has specific numbers in it, likeprint(5 not in goodtreks)and it'll produce a Boolean answer. - If you want odds, start with an odd number and then step by
2. - If you want evens, start with an even number (or 0) and then step by
2.
That's definitely a good place to end it today, since tomorrow starts with the Conditionals, and boy howdy, are my various lists going to get a workout.
Today's Sticker

A Mofusand kitten who is also a donut and also coffee that I got from Tofu Cute.