Chapter 5: Crooks R Us Bank & Lists

Start Here

Chapter 1: In which a cow surprises you

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists (This chapter)

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies

Chapter 9: The Doggy-Nator and List Comprehensions

Chapter 10: In which you Eval the Doggy


Crooks R Us: Give us your money, and then we tell you to @#£$%

You enter Crooks R Us Bank. The place is full, and you see a sign in front of you.

“Crooks R Us Bank. Where we love our customers so much, we will have sex with them. Now give us your bleepin’ money.”

You see something more worrying. A bunch of robbers are in the place, and they are robbing the customers. They aren’t touching the bank employees, for some reason. You run to a bank teller.

“Have you called the police?”

“Why should I?”

“Cause of all these robbers, you know?”

The bank teller picks his nose. “So? We have a professional agreement with the robbers. They don’t rob us, we don’t rob them. Professional courtesy.”

“At least call the police,” you say.

“Why should I? It will cost us thirty pence. The bank exists to make a profit, not solve social ills.”

The Moo-Inator looks into the teller’s window. “I’ll be back.”

In a whisper, he adds, “Really, have to go to the toilet.”

He returns five minutes later, and smashes through the window. He is carrying a big gun, and starts blowing the robbers away. All the robbers run towards him.

The Moo-Inator turns to you. “I can destroy these criminals, but you need to point them to me. Show me where they are hiding, and I will take them out.”

You see there are five robbers, and they have taken cover behind different objects, and are shooting at you. You make a note where each on is hiding.


You know all this already. One thing I haven’t shown you is how to get the length on an list:

The len() function tells you how man elements a list has.

Why did this happen? Because lists start from 0, so the last element is actually 4. The last element of a list is: Length of list – 1

“Can you see the enemy?” says Moo-Inator.

“I can see the last four; but not the first one.”

If you want to see only a few values in your list, Python allows you to do so using the ‘:’ operator. For example, to see all values in the list except for the first:

[1:] means take the 1st value in the list, and show all values after that(including the first). Remember, in Python values start from zero. So if you did:

This is the same as doing:

You can also say you want to view all values up to a number. Say you want to view the first four values:

The general format is: List[x:y] -> You need at least one of x or y, but you can leave the other. If you leave out x, it will start with the 0th element. If you leave out y, it will continue till the last. You can also have both x and y. What if you want to view the middle elements?

The Moo-Inator fires a few bullets at the robbers hiding in the middle of the room. But he hits a politician instead.

“Oh well,” he says. “Bankers, robbers, politicians. Same difference.”

Now for something really cool:

What was that? You can use negative values as the array indices. Since 0 is the first element, -1 is the last one.

So doing a [-1:] says take the last element, and show all values in front of it. Obviously, there is only one. Hence we see [4].

This time, we are saying, starting from the 0th value, show all values before the last one.

What’s the practical use of this? Several times, when you read a file or a list, the first and last values maybe garbage (CRC values, for example). This is a neat tip to get rid of the first and last values in a list:

The Moo-Inator shoots the three robbers hiding in the middle. Final score: You:5, Robbers:0

Before we go, you can do the same thing with strings. Remember our sentence:

We have seen this. If strings are also arrays, we can then do:

Leave a Reply