Lessons ·
Organizing your data
Lists, comprehensions and dictionaries: organize Awa's sales so you can find them and calculate with them.
Transcript
So far, Awa has mostly handled one value at a time. But her shop records dozens of sales every day, and she is not going to create a variable for each one. She needs a cabinet with several boxes, and in Python, this cabinet is called a list.
To create a list, we open a square bracket, write the values separated by commas, then close the bracket. Each value then goes into its box, and the boxes stay in the same order.
And how does she find one particular sale? By the number of its box, which is called the index. And here is the point that surprises everyone: in Python, we start counting from zero. So the first box is number 0, the second is number 1, and the third is number 2.
There is also a very handy trick: the index minus 1 always refers to the last box, however many boxes there are. So Awa doesn't need to count them.
To read a box, we write the name of the list, then the number between square brackets. Here, sales of 0 gives 1500, and sales of minus 1 gives 800.
And when a new sale comes in? She uses the append method, which means add to the end. A method is an action that belongs to an object, and we write it after a dot. Look: a new box appears, and it is number 3.
Finally, the len function, which comes from the word length, counts the boxes. There are now four of them. Notice the difference: four boxes, but the last one is number 3, since we started at zero.
If she only wants to keep the big sales, does she have to write a loop and a condition? She could, but since this need comes up very often, Python offers a shorter way of writing it, called a list comprehension.
It reads almost like a sentence. Take each value v in the list sales, and keep it if it reaches 1000. The square brackets around it show that the result will be a new list.
So Python looks at each box: 1500 is kept, so is 2300, but 800 is not. A new row appears with the two big sales, and the first list, for its part, does not change.
A list works well when the order matters. But to describe an item, Awa doesn't want to have to remember that the price is in box number 1. She prefers a card, where each piece of information has a name, and in Python, this card is called a dictionary.
We write it between curly braces. Each line of the card is made of two elements separated by a colon: on the left the key, in other words the name of the information, and on the right its value. It is like in a real dictionary, where we look up a word to find its definition.
So she asks directly for the price, without wondering where it is? Exactly. She writes the name of the dictionary, then the key between square brackets and quotation marks, and Python gives her 800.
And if she asks for information that is not on the card, like the stock? With square brackets, Python does not find this key, and it stops on an error called KeyError, which means a key error.
When a piece of information may be missing, we therefore prefer the get method. We give it the key we are looking for, then a backup value. If the key doesn't exist, Python shows no error: it simply gives back this backup value, here the word missing.
By bringing the two together, Awa gets her real sales ledger. It is a list, so between square brackets, and each of its lines is a dictionary, between curly braces. Each key plays the role of a column in the ledger. So this is already a data table?
Yes, and she can already calculate with it. This last line takes the amount of each sale, then the sum function adds them up, which gives 3800. This is exactly where the pandas library will start from, in the next module.
In the meantime, try without watching the video again. In the lesson, you will count and add up by yourself, then correct code written by an artificial intelligence. If you get a box number wrong, don't worry: that happens to everyone at the start.
Voices: Google Chirp 3 HD (Despina, Enceladus).
Photos : One Idea LLC, StockSnap, CC0

