list and tuple
In the previous lesson, I wrapped up the section on dictionaries. In this lesson, I’m going to introduce a new section on arrays. In the first part, I’ll be concentrating on the Python built-in types
tuple and how they’re used as arrays.
Here’s a simple example. At address
100, there’s the letter
'A'. At address
101, the letter
'D' are all stored next to each other in four addresses in a row in memory. These same letters could be stored in a different fashion.
A common mechanism for storing things is a linked structure. Here’s an example of that. It requires some pointers. There’s a pointer that points to the spot
205 in memory, which contains
'A', and then another pointer, usually associated with that
205, which points to a second spot containing
'B', which has a pointer to
'D', et cetera.
01:39 Accessing a portion of an array tends to be a quick mechanism, and that has to do with how things are stored together. If every element in the array takes up the same amount of space, then getting to the n-th element just requires a single multiplication and addition inside of the processor.
The address of the n-th element is the start address of the array, plus n times the size of the element. So if you think back to the example I gave of
'A' starting at address
100, getting the third element after
'A', I start with
100, the address of
'A', and then I take 3—the third element—times the size of the element, which in this case, a character is 1 byte. And I add 3 to 100 and I get
103, which is the address of the letter
'D'. CPUs are very good at simple math, so this tends to be very quick.
Python’s array is called a
list. This is a core part of the language and is a built-in type. The name itself is a little misleading. It’s not a linked list, but a mutable dynamic array. Mutable means the elements inside of it can be changed. It’s dynamic because it’s resized on the fly.
03:40 And because the lists are mutable, you can set any position in the list to a different value. I’ve changed the first position from a small letter string to a capital letter string—something different.
It returns what was there. You can also specify a parameter to
.pop()—the index of the thing you wish to have popped off. With these two calls, I’ve popped the last item and the first item, leaving Roman numeral
'II', and the word
'three'. In addition to using the
del keyword, you can call
.remove() on a list and it will look for a matching item and remove it from the list.
Next to the right square bracket is the function
range() returns a iterator of items based on the numbers it’s passed in. In this case, it’ll be the numbers from
for x in in the middle works like a
So this is going to iterate over the numbers
10, assign it to the value
x, and then because of the
x next to the left bracket, that will be the item that gets put inside of the actual list.
This returns items
4. It’s non-inclusive, so you’re actually getting items
3. And remember, it starts at
2:4 is the third item and the fourth item only. You can also index negatively.
That starts from the right-hand side.
-1 gives you the last item in the list. You can also splice without an ending. This starts with item
7 and goes to the end of the list. Because this is zero-based, you get the eighth, ninth, and tenth item in the list.
The list now contains the sorted numbers. Python supports more than one kind of array. In addition to the
list, it also has something called a
tuple is very similar to a
list, but it’s immutable.
08:12 Interestingly enough, you can add something to the end of the tuple. This isn’t really adding something to the end of the tuple. What this is doing is creating a new tuple, which is the combination of the first one and the second one that was added to the end. Python’s just doing this for you.
Become a Member to join the conversation.