Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

list and tuple

00:00 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 list and tuple and how they’re used as arrays.

00:17 An array is a data structure that contains multiple items that are next to each other inside of the computer’s memory. This is referred to as being a contiguous data structure.

00:28 Here’s a simple example. At address 100, there’s the letter 'A'. At address 101, the letter 'B'. So 'A', 'B', 'C', and '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.

00:45 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 'C' and 'D', et cetera.

01:06 This is often referred to as a linked list. Notice that 'A', 'B', 'C', and 'D' are not stored next to each other—they’re in four completely different addresses in memory.

01:16 This kind of data structure also requires more space because in addition to what you’re storing—the letter 'A'—you need a pointer to the next container.

01:27 Most programming languages support some sort of array mechanism. Arrays may or may not be typed. Typing indicates that all of the contents of the array are of the same kind.

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.

01:57 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', 'B', 'C', and 'D' with '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.

02:37 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.

02:56 And it’s an array because it’s stored continuously in memory. Unlike some languages, list is not typed, so although it is an array, each element can be of a different type.

03:09 You declare a list in Python using the square brackets.

03:17 Here’s a list of three strings. You can access any item in the list using the square brackets to indicate the position that you’re looking for. Lists are zero-indexed.

03:29 That means position 0 is the first one. The len() function returns the number of items in the list.

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.

03:53 You can use the keyword del to remove an item from the list. This removes position 1, which is the second position, so the string 'two' is no longer in the list.

04:07 Everything in Python is an object. Lists are no different. And the list object includes methods. The first method I’ll show you is .append().

04:17 .append() adds an item to the end of the list. If you don’t want to put it to the end, you can use the .insert() method, where you specify where it should be inserted.

04:31 And again, because of zero-indexing, position 1 ends up being the second spot in the list. The .pop() method pops an item off the end of the list.

04:42 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.

05:11 This found the string "three" from the list and removed the associated position. Lists can also be created using something called a comprehension.

05:20 This allows you to calculate values to be put inside of the list as you create the list. This is also done with square brackets and then something that looks like a for loop inside of it.

05:37 For list comprehensions, I like to look at the outsides first. Next to the first square bracket, it says x. This will be creating the list based on the contents of x.

05:47 Next to the right square bracket is the function range(). range() returns a iterator of items based on the numbers it’s passed in. In this case, it’ll be the numbers from 1 to 10. The for x in in the middle works like a for loop.

06:03 So this is going to iterate over the numbers 1 to 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.

06:17 Here’s the result—the numbers 1 to 10. You can get at pieces of the list by slicing it. You do that with the colon operator (:) inside of the index.

06:29 This returns items 2 through 4. It’s non-inclusive, so you’re actually getting items 2 and 3. And remember, it starts at 0, so 2:4 is the third item and the fourth item only. You can also index negatively.

06:46 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.

07:05 If you want to combine a list, you can use the .extend() function to tack the contents of a list onto the end of this list.

07:18 And finally, the built-in .sort() method will in-place sort the list.

07:26 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. A tuple is very similar to a list, but it’s immutable.

07:41 This means all of the elements are defined at creation time and cannot be changed. Tuples are defined using the round brackets, or parentheses.

07:57 Similar to lists, you can get at certain index items in the tuple, but unlike a list, you cannot change it. You can’t delete an item either.

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.

08:30 I can show you how this happens with the id() function. This is the ID of the tuple letters. If I then append another item to the end of it

08:45 and take the id() of letters again, you’ll notice that the ID is different. The variable letters has been replaced with a new tuple containing the letters 'a' through 'f'.

09:00 The collections library has a function called namedtuple that allows you to create tuples that are kind of a hybrid between a tuple and a class.

09:17 Here, I’ve created a namedtuple called Point that’s expecting two things inside of it: 'x' and 'y'. This is a great way of containing a coordinate in a graphing system.

09:30 If you look at Point itself, it’s a class. I then can create an instance of a Point the same way I would with a data object.

09:40 The first value passed into the constructor gets assigned to the first thing in the namedtuple—in this case, x—and the second into the second.

09:50 Printing out the Point, you can see this assignment has happened. Just like a regular tuple, I can access this with subscripts, but I can also access it by name.

10:01 This makes it easier to see things inside of the code. Also like a tuple, I can pull items out of it chained together. This got me the x and y out of Point p.

10:17 Python has other ways of representing arrays as well. In the next lesson, I’ll show you how to use the array library to create typed arrays and how strings are actually arrays themselves.

Become a Member to join the conversation.