All language subtitles for 26. List Slicing

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Welcome back.

Let's continue with lists now so far the square brackets.

We've seen them before right.

We saw them when working with strings and just like strings.

Lists are quite similar in that we can use list slicing.

If you remember with String slicing we have things like hello.

And we were able to assign into a string let's say a variable string and we could do a string and then

slice.

So do something like this where we had the start the stop and then the step through so that we start

at index of 0 and end at index of 2 and then go one by one.

So list slicing is also available to us.

So let's make this card a little bit bigger and you can actually just make things cleaner by formatting

it this way.

All right.

So what should we add to our cart.

We'll also add some toys and then you know what Amazon does groceries now so let's add some Delicious

grapes.

Awesome.

Now let's say I wanted to get every single item in the cart while we just simply do this and we have

our entire list.

But let's use some list slicing let's say I wanted to grab from the first item to the second item.

If I click Run I get notebooks and sunglasses maybe I want to go all the way till the end.

But skip every second one I get notebooks and toys.

We started zero.

We step over to toys and we step over and we're done.

Awesome.

And this is something that we've already seen when talking with strings OK but here is where it gets

interesting.

Remember how I said that strings are immutable.

That means we can't change them.

Right.

And we talked about this when we had a string like Hello.

I couldn't do greet zero equals to let's say Z.

I'd get an error here.

I get SDR object does not support item assignment.

It's immutable.

But the interesting thing with lists is that they are mutable so that if I change my Amazon cart and

say that you know what I don't really want notebooks so I'm going to grab the first item which is notebooks

and instead of notebooks.

Hi.

What do we want.

We'll say we want a new laptop.

It's a big upgrade.

When I print this Amazon cart

look at that.

I'm able to change this list.

And it didn't give me an error in that sense.

Lists are immutable.

We simply replace on the memory bookshelf of our computer notebooks.

And I say Hey change it to a laptop.

And they let us do that OK.

So that's awesome.

But let's try something here.

What if I create a now they're print here and in the Amazon cart I'll use list slicing to let's say

I want to item from index of one all the way until index of three let's run this and see what happens.

Try and guess

is that what you expected.

Let's go through this code when we get to line 10.

We grab the Amazon cart which has been updated with laptop and I grab item 1 2 3 2 that is sunglasses

to toys so 0 1 2 and then we stop at 3 just to make this easier to understand.

Let's start off with zero here so that we see that laptop has been changed and then here in the second

one online eleven we print the Amazon cart but hold on a second.

This list did not change and that is because with list slicing we're creating a new list a new copy

of this list.

So here we're creating an entirely new list so that I could actually assign it to a variable new cart

is going to be Amazon cart and let's just do the same thing here and new cart is an entirely new list

on its own.

I could change a new cart let's say zero into let's say gum and if I print this you see that I have

two new separate lists but a list is mutable because I can change whatever is at the index anytime I

want.

And every time we do list slicing we create a new copy of that list but I have a tricky question for

you here what happens if I just do this

if I run this.

Did you see that instead of slicing my list I simply said that new cart is going to equal the Amazon

cart and I changed a new cart index of 0 2 equal to gum but now my Amazon cart got modified as well.

Why is that and this is a bit of a tricky question that you might encounter in an interview.

The reason is that right now the way that I did equals means that hey new cart is going to equal Amazon

cart.

And what does Amazon cart equal to.

Well Amazon cart points somewhere in memory in our machines that says hey this is what Amazon cart is.

So because here we're not copying the list.

Instead we're just saying hey the value of new card is whatever is in the memory of Amazon cart we now

when we modify a new cart are simply changing the Amazon cart all the way back from here.

So this is an important concept.

If you want to let's say copy a list then you do something like this where you copy the entire list

and this is something that you'll see a lot in code bases but this line is saying hey I want to create

a copy use list slicing to copy this Amazon cart and it's going to equal new cart so that now if I run

this you'll see that the original Amazon cart stays the same.

But the new cart is now something different.

This is a quick Gotcha.

They just have to get used to but it's an important concept.

This idea of copying vs. modifying the list and it's something we'll explore a little bit more in the

next couple of videos for now take a break and I'll see you in the next one by.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.