Welcome everybody, you're watching mr fugu data science. We're doing lists of lists today. But
this is going to be a little different than the previous video. As always thank you to the recent
subscribers and viewers i greatly appreciate it. Hit me up on instagram and twitter. Also,
if you would like to help support the channel and you thought this was somewhat useful hit me up on
buy me a coffee. You got only two imports today. Pretty easy pandas and time. The first list that
we're going to deal with: it's uneven in size. If you notice that each one of the nested portions
are different lengths. The second list b has two nested lists. The first are people's names
the second are just made up addresses, the third list c is full of mixed types. Last one right here
are your first and last name and your occupation. And then you have these tuples which are your age.
We're gonna try to parse through that, and figure out how we can put it all together
and line them up. Let's start with the first list right here. And we're going
to notice something. So here's the first index position for each one of the list in a. So 1 5 8
and 10. But let's do something, and notice this error. We looked at index position three;
this does not have index position three. This does not have index position three. This does not.
You could have out of range error. So how do you handle this? Consider using a try except. What
i did was create the ability for you to throw in your list with the index position that you
have of interest, iterate through the range length of it. Allow you to iterate through
and put your index position. And if you raise an exception we'll cover it by appending to the
new list, a none value so we could retain the same amount of elements for our list.
There we are. What we have is at index position 2. Let's do this; so at index position two we're
taking the number three, the number seven. This one does not have one. This one does not have
one. If we did it for not three when it was just this number four. But if we went out of bounds,
it went fours: we get all nones. Okay let's check and see how fast this actually runs,
all right. There's the speed it takes on my older i7 macbook. Let's decide how we can
do some flattening. So instead of going from a nested list we flatten out into an individual
list. I'm going to show multiple methods for this. Let's do a list comprehension for the first one
since this is nested. I have to do two loops with inside of this. Here's the first portion
we have that's flattened out. As a hint: list comprehensions are faster than regular looping
typically. But, since these are so small it's going to be negligible, the amount of time savings
you're going to see. Let's do our second method of un-nesting using a regular loop, let's print
out the same thing but, we're only interested in time. If you notice here 765 nanoseconds versus
1.2 microseconds. This is definitely faster. Let's look at a third example from iter tools,
a method that we're going to use is called chaining. And pay attention to this star right
here. Let's cut and paste this and figure out how fast it is compared to what we did above.
So it's a little faster than the list comprehension. I would say if you had a
lot of data you could run it and see. Because this may vary for each one of these iterations. If you
did it another time. Just be aware of that. Let's unpack this but, let's do it a little differently:
where we're taking the individual index positions and flattening it out with this star notation.
This is only taking the first two entries. It's not taking the whole thing. Let's look at another
way to do this with extending a list. Okay this isn't bad, this would just give us the same exact
thing that we did above. Let's take the list but, only take entries relating to length two
or more. We're going to throw this into a list comprehension just like we did above. Here's
the two list of lists that we have with this specific length. I used this bracket around it,
so we could retain the same ordering. If you take it away it'll just do a flattened list. We can
convert this into a list of tuples where we're taking our names and our addresses. Look at this
real quick. If you pay attention here. Here's the names of the people, here's the addresses. Simply
we can do a zip, putting these together based on their index positions and that's what you obtain
here as tuples. We can then do something where we're interested in taking this list d. Look
at what d looks like real fast. Here's d. This is first name, last name and their occupation.
And here's the relevant ages. These are tuples, these are lists. We have to figure out how we're
going to manipulate this. It's almost in the formatting that we want but not quite
yet. Let's move on to the next portion: which is going to use some of this code
and we need to modify it in a few steps. Here's what we got so far with the names. That looks
fine, and then we append it in here's. The edges that we have, i don't need to throw it into this.
There we are. After we fix our mistake,
we're almost there. What else do we do? We got to deal with these ages though.
There. Now we got everything together. But, let's actually see what this looks like, if we time
this. Let's go over what happened. So i had to verify... Do some kind of checking. So i decided
to use the list since we had list first, tuples, anything low is a list. I could just take it:
append it as a dictionary with its names and the iterator as its values, chuck it in here, anything
else which would be your tuples i'm throwing it in here. But i'm only taking the second element
of the tuple which is index one. We could just take the age and i just throw it into two data
frames and concatenate. So there we go. We're at milliseconds which is kind of slow, for what we're
doing. Let's look is something else similar this time. Let's use a list comprehension. There we
go. This is looking like what we had above. Now we have to still do our second part, so just cut and
paste this. I fill in a new part, so now let's get that new part in. I iterated through there real
quick, did something similar to what we did in the last one with the list. But, instead i'm comparing
with the dictionaries. Now, since the other ones are tuples i'm appending those directly in
i'm converting the tuples into a dictionary, appending that. Now i'll throw it into data frames
and there you go. But, that's not the end of the story. Let's take all of this
and let's time it as well. So we're at 1.7 milliseconds and the other one was 1.64. Doing
this with a larger data set would allow you to see if there's any discrepancies or if they're in fact
fairly close. Let's play around with our list c. This is what c looks like. It was the mixed type
and for each one of these lists i printed off the length so you can see that this was an empty list.
And let's do some playing around. First thing we're going to do is take these strings from each
list and your index position. I'm going to take this string, this string, this string and this
string. And i'm going to keep the index position so we know what list it actually came from okay.
There we go. And we paste this in there. Then we can actually get it to print out. Since it looks
like i manipulated above somewhere. So there we are. Here's the index position showing which array
it came from, and here's the string that was printed off. Let's try adding some elements to
this at a specific index position. For instance, here's the original array of a. And we're going to
add at array number three, or index number three. We're going to add 11, and that's what's placed in
here at the end of that particular list. So you're just appending to the end of the list and you're
going to the third indexed list from a. We can update a value for a nested list. For instance,
here's the original for e, that we're creating right here. Go into the first one right here.
The first index list, and put two. But notice that it's actually updating and changing the whole list
and replacing it with two. It's not adding it in there. Now we could insert at a specific location.
For instance, we could take e, that we had above that we just replaced with that number two. And we
can go into the third list and replace the first indexed position here, okay, with yay all right.
And that's what's going on here. So it doesn't overwrite the seven, it instead puts yay. The
string of yay at that index position. So you're going to the third index list first position.
You can merge one list into another list by using extend. You're basically
flattening out a list. What we're doing is going into this list here, which is our e from above.
And you're going to the third location, for your index which is this list or pardon me
this list. Then you're putting in this right here. which is getting placed here. We can then
move on to removing using pop. Here we're going into the first, right here. And what happens is,
it removes this. If you pay attention here. But, notice something really interesting:
you can use that pop. And we're going to store it with this right here. We're going to take c,
and that's what it looks like okay, right here. And we're going to pop. Let's look what happens
when you routinely pop this okay. The first time we do this we're going to notice that we're taking
out this right here. Then the second time you do it you're going to end up returning this.
You do it again and you return this, and what you're doing is you're not returning anything
else. But what are you actually popping? This is what's coming out. And then this is what's coming
out. And then this, and so on. If you keep doing it; remember the pop is just returning
what you're removing. If you want to actually return everything except what you deleted: you
use the dell for deletion. We're going to use the first, first index position which is the second
list. And then the first index position which is this. We're going to notice that we have chicken
and frog and we've removed dog. Now what if we want to remove chicken but, we don't remember
exactly where it is. We know that it's in this particular list, which is indexed here. But,
we don't know what the other index is inside of that list. That's fine, you just use remove.
As long as you know what list it came from for its index position. Finally, we can use something from
iter tools called cycle. We start with this list, which is people's names and then we just have some
longer list which is just some made up numbers. You have four entries here and you have five here.
This is allowing you to do something interesting. You call the cycle on the longer list,
pardon me. On the shorter list. And we're zipping together both of these lists, and you'll have it a
redundant data here which is taking your Jose, and going to 10 which is here. Your Shawn at 49 which
is here. Then it goes to the end which is Genie and 19 and then the next thing you have is Jose
gets recycled and goes to eight. Some situations you may need this. I'm not sure really when but,
it's available to you. But here's just some simple examples to get you started on finessing list of
lists in your journeys into python. As always i hope this brought utility to someone. Please like,
share and subscribe. And if you subscribe turn on that notification bell. Thank you for watching;
don't forget if you thought this had some utility consider buying me a coffee and
helping support this channel. Otherwise, hit me up for twitter or instagram. bye