Learning Python, Day 5 — "The List Ninja"
Okay, Friday was a total wash, and the weekend was the weekend, but I am back and ready for lists!
07 — The List Ninja
I do like lists. It's the only way I manage to keep track of everything. So when you tell me how to make lists of things in Python, I am pleased. Mmmm. Lists. I'm categorising things. I'm in heaven.
However, "List Ninja"? No. Unless I'm secretly making lists for the daimyō who has hired me, I am not a list ninja. I am a list person (job title irrelevant).
And these "Ninja Traits"? What.
ninjaTraits = [ "silent", "sneaky",
"patient", "humble",
"strong" ]
Remembering that 0 is the first item in a list though is going to bug me, though. But that is because I am pedantic.
In fact, one of my lists made that very clear.
mytraits = ["smart", "boring", "pedantic"]
othertraits = ["dumb", "interesting", "pedantic"]
mytraits == othertraits
>> False
And remembering that if I want the full range to appear, I have to add one extra number in my range (using 1:3 when I want items 1 to 2). But I figure this is all something that will happen over time and it'll become second nature to me, like what c1b means in knitting or what bb.MoveObjects on does in Sims 4.
(Yeah, I know there's integer: to give me all the items to the end of the list, but I'm talking about, like, :integer and integer:integer. So annoying.)
I also like checking if an item is in a list, but the test examples are boring. I'm not going to check if I've written the right colours. Or the right shapes. Or whatever.
hipdirectors = ["John Waters",
"David Lynch",
"David Cronenberg",
"Brandon Cronenberg",
"Ryan Coogler",
"Guillermo del Toro"]
"Brett Ratner" in hipdirectors
>> False
Yeah, buddy, you're never in the hipdirectors list.
I'll also have to remember that when I add an item to the list, that it won't return a result unless I print() the list. As much as I love Rose Glass's films, she doesn't need to be in the hipdirectors list twice.
But I can remove them in a variety of ways, so if I accidentally put Rose Glass in, like, 20 times, I can just del hipdirectors[8:] and it'll get rid of all but one of her.
Day 5 — Results
- Making a list is
listname = [item1, item2, item3, etc]. - Pulling out a single item from a list is
listname[integer]. - Lists start at
0. This is annoying. - Pulling out a range of items from a list is 'listname[integer:integer]'.
- When pulling out items from a list, you have to go up one integer in order to get everything you want. This is also annoying.
- Pulling out all the items to the end of the list from a specific item is
listname[integer:]. - Pulling out all the items from the start of the list to a specific item is
listname[:integer]. - Seeing if an item is in a list is
item in list, which will give youTrueorFalse. Same goes foritem not in list. - Brett Ratner is not a hip director.
- Adding items to a list is
listname = listname + [items]orlistname += [items]. - This will not give you a response, so you will keep adding it to the list because you will forget if you clicked Run.
- You can also use
listname.append(item). - Removing items from a list is
listname.remove(item). - You can also use
del list[integer]to remove a specific item based on its index integer. - All the range bits can also be used to remove items from lists.
- None of this makes you a ninja.
Tomorrow, it's time for if statements. Oh, I am going to keep my hipdirectors list going and give you all terrrrrrible opinions.
Today's Sticker

Floral David Lynch from Megomobile.