Python for Beginners, Part 12: Lists of Things by@cleverprogrammer

Python for Beginners, Part 12: Lists of Things

In recent previous tutorials, we talked about functions, loops, primitive data types, and string slicing. In this video I'm gonna talk about lists, lists of things and what the heck are lists. Lists are used to store more than one items in a single variable. Lists are one of four built-in data types in Python and is used to store collections of data.
image
Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

youtube social iconinstagram social icontwitter social iconlinkedin social iconfacebook social icon

Let's cover the list data structure and why it's so wonderful.

In case you missed it, here are the previous parts of the series

Transcript

0:00 Hey guys, what's up?

0:01 So in the last video I talked about string slicing.

0:06 In this video I'm gonna talk about lists, okay?

0:08 Lists of things and what the heck are lists.

0:11 Lists are awesome.

0:12 However, in the last video I left you guys with a question.

0:16 I said, take this,

0:20 right, and parse it.

0:21 And basically XBox 360, pull that out,

0:25 save it in a variable like product.

0:27 Pull this out, save it in a variable like price.

0:30 And save this in a variable like condition.

0:32 Right, so when you have your Excel sheet open,

0:35 it looks really nice, it'll look like this.

0:43 My solution, I'm gonna do it in a different way

0:47 because if you try to use it as index, this is the solution.

0:50 So you can pause the video, think about how it works.

0:53 That's how you'll pull the 150

0:55 and using the same logic, you'll pull the New.

0:57 However, index is a very ugly method to use

1:01 for getting, parsing the string.

1:04 What I want to show you guys is this method called find.

1:07 Okay, that's a very nice method.

1:09 Let's say we take the data, and I do data.find

1:14 and find, it shows you the bottom what it takes in

1:17 as the function, it takes in the substring

1:21 and then it takes in optional arguments, like start and n.

1:26 So I'm gonna say data.find

1:29 and I'll give it

1:30 substring pite.

1:33 What a substring means is that, that's something

1:37 that exists in the original string.

1:41 For example,

1:44 I have to give find,

1:45 the function find or the method find,

1:47 I have to give it something that exists within this.

1:50 If I gave it something that doesn't exist within it

1:52 like banana,

1:54 it would say negative one,

1:56 like I didn't find anything.

1:58 But if I gave it a pite, you can see that it finds a nine.

2:04 And another thing you can give it is how you solve...

2:09 Is you can give it where to start from, okay?

2:12 So I can say, for example,

2:15 start looking after ten

2:19 or look for pite,

2:21 but I want you to start at string nine,

2:24 okay?

2:25 Or I want you to start at string ten, alright?

2:29 And now it finds a pite at location 15.

2:32 So if I do data, and if you count it up,

2:35 you'll see that this is the pite

2:37 and it is at indeed at location 15.

2:39 If I do data(15), you'll see that

2:41 it gives you back that pite.

2:45 Right?

2:47 Okay, so by getting that second pite,

2:51 now you can get the value in between the first pite

2:54 and the second pite by saying something like,

2:59 start from finding the first pite

3:03 and then go up to

3:06 finding the second pite.

3:10 Right?

3:11 So I can say, finding,

3:14 or start from,

3:17 or look for pite.

3:19 And then start from ten, for example, right?

3:24 And this gives you this guy here.

3:26 And just to make sure we don't

3:27 include the pite in the first one

3:29 we can just start from plus one with that.

3:31 Okay?

3:32 So that gives you in between the first and the second pite

3:34 you get probably like 150.

3:36 Okay, so,

3:38 the reason why I went even more in depth in strings

3:41 even when the video called lists

3:43 is because a lot of what you learn from strings

3:47 is applicable to lists, okay?

3:49 So, let's take a list and call it groceries.

3:54 And I will have apple, and I foreshadowed this

3:57 for you guys a little bit...

3:59 Banana--and you can also have integers.

4:01 Although usually you try to

4:02 put the same kind of thing in the list.

4:06 But sure, let's put five in there and six in there

4:09 and then oranges or something, okay?

4:12 Cool.

4:13 Now, if I wanna

4:15 take this list

4:17 and I wanna get the first element and the second element,

4:19 if it was just a string,

4:22 right?

4:22 Like the reason why lists are useful is 'cause

4:24 you can use it to store multiple variables.

4:29 Like for example, if you wanted to use variables

4:31 to store this, aww shh--

4:34 It would be a little...

4:38 It would be weird.

4:39 'Cause you will say groceries, zero is equal to apple.

4:43 You would say...

4:51 Groceries of one are banana.

4:53 And so then when you do groceries of zero,

4:55 you get back apple.

4:57 And when you do groceries of one,

5:00 you get back banana.

5:02 But lists make it, make that thing,

5:05 for you in a really easy way, okay?

5:09 So now, if I do groceries and if I do groceries of zero,

5:14 I get back apple, okay?

5:16 And if I do groceries of one, I get back banana.

5:19 Because apple is in the zero position.

5:22 Banana is in the first position.

5:25 This is in the second, third, fourth position, okay?

5:30 So lists make it really nice for you.

5:33 Imagine if you were trying to store data for a race.

5:36 So like, four of your friends ran really fast

5:40 and you wanted to record who came first, who came second.

5:43 So you can save

5:46 race is equal to

5:49 like John came first, and then Bob came second,

5:53 and then, you know, whatever, like Timothy came in third,

5:59 uh, Timothy is pretty slow. (laughs)

6:03 So we can say like, okay, who came in first?

6:07 We can ask that question.

6:08 We get back John.

6:09 We can say, who came in second?

6:11 We get back Bob.

6:12 And I can say, who came in third?

6:14 Or I can say who came in last?

6:17 That negative one gets you the last element.

6:19 And this answers all my questions.

6:22 So just like how were able to slice strings,

6:26 we're able to slice

6:28 lists, okay?

6:30 But notice, lists is nicer for elements.

6:33 So for example,

6:35 XBox 360 and all this stuff,

6:38 I'll show you guys a really cool trick you can do with lists

6:41 to break that up into pieces.

6:43 So like for example,

6:46 if I gave you a string like this, right,

6:48 what is going on?

6:50 Notice that there are a lot of spaces in between, right?

6:52 Let's just replace those spaces,

6:54 just so it's more visual,

6:56 let's replace those space with that,

6:59 and let's say we want to get the what part separately,

7:02 is part separately, going part separately,

7:04 and on part separately,

7:07 without having any of the dashes in the middle.

7:10 There's a method we can use called .split, okay?

7:14 And we can pass to .split what we want to split by.

7:19 So I can say something like this,

7:21 I want to split by the dashes, okay?

7:23 So check it out, now I got what is going on, right?

7:27 This is a list

7:30 of strings.

7:31 Now the data complexity is getting a little bit higher,

7:34 it's a list containing strings inside of it.

7:39 Okay, so, I can call this like,

7:43 you know,

7:46 greeting or something, it doesn't matter,

7:48 and if I do greeting, I can say

7:50 What's the first word of greeting?

7:53 What's the second word?

7:58 Whoops.

8:02 Greeting, right, what's the second word?

8:05 And for example, I can say what's the last word?

8:09 And it'll give me what is going on,

8:11 on being the last word, right?

8:13 Now, I can also do string slicing just like

8:17 how I can do in strings.

8:19 So I have start, I have stop,

8:22 and I have step.

8:24 So if I take the race one again,

8:27 I can say go from zero all the way to the end.

8:31 I can say the same way, like this,

8:35 this is like optional, right?

8:38 It starts with zero and it ends at zero,

8:40 or ends at the last one by default.

8:43 If I wanna get from John to Bob,

8:46 I can say like give me the

8:47 first two...

8:50 first two contestants of the race.

8:53 And it will give me John and Bob.

8:55 Or I'll say give me everybody but the last guy

8:58 so I can ask that question to my list, just like that.

9:04 Okay, now to answer your guys' question

9:06 of how to reverse the string,

9:08 it's the same you can reverse the list as well.

9:12 So, lemme show you guys.

9:14 There are a couple of different ways,

9:15 but I'm gonna show you guys the nicest, the fanciest way.

9:20 So let's take this list

9:23 and I wanna step by...

9:26 Sorry, I wanna start at the default, which is zero.

9:29 I don't have to write zero,

9:30 but I'll just write it for you guys.

9:32 I want to go all the way to the end

9:34 and I don't have to write this, okay?

9:36 So I'll just leave it blank here.

9:39 And for the last part, for step,

9:42 instead of saying step by one,

9:44 which will just give me the whole thing.

9:47 Instead of saying give me every other element,

9:49 so like give me John and then Timothy

9:52 I will say

9:55 step by negative one.

9:57 So check this out,

10:00 let's remove

10:03 this,

10:05 right?

10:06 And let's remove that guy, and let's do that,

10:09 and you will see how it steps.

10:14 It just reversed the list.

10:17 If we have

10:18 the same string, like

10:20 let's say we have data I can do the same way and

10:25 reverse everything in data.

10:28 So you can see XBox 360 and New and 150

10:31 are all written backwards, okay?

10:35 So that's how you would like reverse something.

10:38 Now I want to talk about

10:43 split again,

10:44 and I want to talk about one very important method

10:48 called a pen for a list.

10:50 One of the most commonly used things

10:53 that any programmer does in any language, okay?

10:57 So split is like Java, Cs, everybody uses that.

11:01 It just might not be called split.

11:02 And the same thing in Python, everybody uses split

11:05 and the next method I'm about to show you, pen.

11:07 Very common for all the games you make,

11:10 for everything that you do, it's going to be like

11:11 one of the most common thing you use,

11:13 especially a pen, so pay attention.

11:15 So I'm gonna show you

11:18 the method called

11:20 the split, okay?

11:22 So I've shown you a little bit of that already.

11:23 So let's say I take data and I wanna break it up,

11:27 right, product, price, condition.

11:31 Very easy way to do it, very easy way to do it split.

11:33 So let's use data.split,

11:36 and I wanna say split by

11:39 pite.

11:41 Boom, look at that.

11:42 How nice is that, right?

11:44 It automatically just split it by that

11:46 and you can store that in

11:50 details or something.

11:52 And now if I do details of zero,

11:57 and I can set that equal to product.

12:00 So now if I do product, I get that.

12:03 I can say

12:05 price is equal to

12:07 details

12:09 of one

12:10 and I can say condition

12:13 is equal to details

12:14 of two.

12:16 Okay, price, condition,

12:19 boom,

12:20 okay?

12:21 So that was like some really nice ways to get this done.

12:24 In the next video I will talk about a pen.

12:27 So stay tuned.

react to story with heart
react to story with light
react to story with boat
react to story with money

Related Stories

L O A D I N G
. . . comments & more!