Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Lists

The final foundational Python data type we will cover is the list. A list is a collection of data, possibly of varying types. We'll take a first look at lists in this lesson. As you'll see later on, lists are the foundation for many other, more complex data structures.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


2.4 Lists

The third and final data type that we're gonna talk about in this section is going to be one of my favorites, and it is a compound data type known as a list. Now a list is really just a way to group together other values. And those other values could be anything. Those other values could be strings or numbers or other lists or even other data types that we'll talk about later on in the course. So it's very versatile. And you can use it to group together different types of data type. Different types of data, although they are typically used to combine similar types of data, so maybe a list of all integers or a list of all strings, but you don't have to be limited to that. You can put any sort of data types together. So let's go ahead and see what a list looks like. So first let's create one, so let's give it a name. We'll just call it list. And we'll set that equal to and the syntax that we use to denote a list is going to be the square brackets. So we're going to put within here some sort of list of date. Now I can put any sort of data type in here, so I'm just gonna use numbers to begin with. And then I am going to start to show you some cool operations you can do with them. So let's just start to put some numbers in here we'll say two four six eight and 10. And now as you can see here when I put different values in there they are separated by a comma so now I have two, four, six, eight and ten, so I have five integer values within this list. So if I were to hit Enter now, as you can see I can now type in list and I get back the list of digits. So just like a string like we talked about in the previous lesson, I can start to pick this apart a little bit, and slice it and I dice it a little bit. So let's go ahead and see how we can get access to individual items within this list. So once again we can use the name of the variable, and then we can use the square brackets to say, give me the value at a particular index. Now remember, just like strings, lists are zero indexed, so the first digit here, two, has an index of zero. And then one, two, three, four at ten, so I can say I want the value at list index four, and that's going to give me ten. And once again, if I were to ask for something outside the bounds, I'm going to get an index out of range error, so just kind of keep an eye on that as well. Now similarly to how we were checking the length of a string we can do a similar things with lists. I can use the len function and I can pass into it the list that I'm working with, and I can go ahead and hit Enter and as you can see here it is five or has a length of five. Now if I wanted to get a range or I wanted to slice my list I can once again say list and then I could use my square brackets and then I can use the slicing notation, where I can give it the beginning index, so in this case we'll say, 0:, and then the ending index, we'll say 2. And it's going to give me a list back that represents that particular range. In this case it's 2 and 4. And I can do it once again. I can say from zero or maybe say from two to and I don't specify anything means I'm just going to go to the end. So I'm going to get six eight and ten, and then I can also skip the first one which means we're going to assume we start from the beginning. And only go to three so that way we're going to get back two four and six so that's quite nice. Now if I wanted to get the entire thing back that way I could also just leave out anything and its gonna give me back once again two four six eight and ten, now what happens if we have two lists? Okay let's say I have list as we see here, I have two, four, six, eight, ten and I have list2, which is going to be equal to 12, 14, 16, and 18. What if I wanted to combine those two lists? Why it's quite simple actually, all I have to do is say list + list2. And we're gonna get back the value of a list where we have all the integer values from 2 all the way up to 18. So that's how we can combine, or concatenate, lists very similarly to how we were working with strings before. Now what happens if we wanted to add something to a particular list? So let's say I was working with list2, in this case which as you can see here, still contains 12, 14, 16, 18. Let's say I wanted to add something to this list or append it onto the end. How would I do that? Well, I would use the list2 as my variable name, that means that's the particular list that I'm working with. And then I want to use the append function to append the number onto the N, and in this case I want to append 20. Now that's going to actually change the value of list2. So if I were to look at the value now, it now goes from 12 all the way up to 20. Now what if I wanted to change a value, because in this case lists are actually mutable, which means you can modify their values, whereas strings are not. So if I wanted to do something like list2, and I wanted to change the second digit from 14 to 24. I could specify what the index is of the value I want to get and this is going to be at index 1, because remember 12 is index 0, 14 is index 1. And then I can set that equal to 24. So if I were to request list2 again, as you can see, that second value has now been changed from 14 to 24. And these are all fine examples, but remember, as I said before, you're not limited to putting all the same data types as values into a list. You can combine things together. So let's create another one. Let's say list three is going to be equal to a list, and this time, we're going to say one, two, and how about a string of three and a list of four, five, and six. So this may look a little strange, but if you kind of look at it closely, it makes sense. So we have the outer square bracket denoting that list three is going to be a list. The first value is the integer one. The second value is the integer two, the third value is the string three. And the fourth value is another list of four five six. So if I hit enter, and then ask for a list three back, I'm going to get that back. So I have two integers, a string, and another list. So as I said before you're not limited to putting just the same data types all by themselves into a list. You can really have some fun and combine some things together. Now, just because you can do that in the same vein as just because you could drive your car with your knees doesn't necessarily make it a good idea. So I would definitely urge you to kind of keep things separated when they make sense. So I would ultimately not just create random lists and put a bunch of random data types in there. I would really create lists for specific purposes, to contain specific types of data that are represented by a very good variable name. Now just kinda keep that in mind but Python is extremely flexible and will allow you to do things like this. Now one of the thing that's interesting to note here when it comes to this last example, how do I get access to individual values in this list? Well, let's think about that. Let's say I wanted to get access to something in this list here. Well, what's the index of this list? Well, we start at zero, then we have one two and three. So if I were to say list three give me the value that's at index three. So what am I going to get back? Well, I'm going to get another list here. So that's fine. Well what if I wanted to get the second value of that list, being five here, which is at index zero and one. So what I could do in this case is I could say list three. And then the result coming back, I want to get the first index of that one. Now this is not really going to work because I used the wrong variable name. So, this is list three. Give me the value at index three, and then give me the value at index one. And there you go, we now have five. So, there you have it, that is the basics of the list of being able to create lists, initialize them, and add things to them. Oh, and one final thing, let's talk about removing values from lists as well. Let's say I was working with list2 again, and where I have 12, 24, 16, 18, and 20, let's say I wanted to remove this value of 16. So how would I do something like that? Well it just so happens that removing a value from this particular list is a little bit more complicated than just using a function to say, append something on to the end. So if I wanted to get rid of this 16, I could do it in a couple of different ways. The first thing that I can do is, yes, use the remove function. So I could say, list2.remove. And I could remove the value of 16. Now, if I were to do that, and then respond back with list 2. Yes, it did get rid of that value of 16. But that can seem a little bit confusing. Because I have to specify a value, but what if at the point in my application, I don't necessarily know the value that I want to remove, but I know the index. So that leaves a little bit of an issue here, because the remove function is gonna take in the actual value that I want to remove. And not only does it remove a value, it removes the first occurrence of a value. So just to prove that to you I'm gonna specify list2, and I'm going to append a 22. So now you can see a list2 gives back my new list with 22 on the end. And then I'm going to do that again and I will say append 22. So now list two has two of the number 22 on the end. So lets say I wanted to remove 22, I could say dot remove now I could say 22 now what exactly is going to happen if I were to specify list two. Now it definitely got rid of one of them and it happens it just so happens that it got rid of the first one so now I can remove by value, and it's gonna remove that specific value or the first occurrence of that value. But what if I wanted to remove this 24 now, but I didn't necessarily know that it was the value of 24, I only knew it was at index two? So how could I do something like that? Well for something like that we can use what's called the delete function, or the D-E-L, del function. And I can pass into it my list. So in this case, list2 with a specified index of what I want to get rid of. But I want to get rid of list1. Excuse me, and I want to get rid of list2 index 1. So now if I say list2, as you can see here, I got rid of the 24, which was originally at index 2 or 24. So there you have it. That's really the basics of going about creating lists and initializing them with values, and then ultimately getting things appended onto the end removed out of that particular list. Now having just these three very simple data types, numbers, strings and lists, you are fully ready to write an application. Albeit it might be a little bit dull, so I'm gonna give you a little bit of a bonus lesson next to introduce to you a very interesting topic that's really going to help you build a little bit more of a fun interactive application which we'll do next.

Back to the top