let's turn our attention to collections
quite frequently almost in any type of
application you might be building you're
going to be working with some form of a
collection
now Python supports a couple three
different types of collections we're
gonna be taking a look at two here which
will be our lists and our arrays so
let's start off by taking a look at
lists the way that we set up a list is
by using that square bracket operator
that we're seeing up at the very top
here now this is something that is
pretty common throughout most
programming languages that that square
bracket is going to indicate some form
of a collection frequently it's going to
be in array in Python it's going to be a
list now a list is going to be a
collection of items and those items can
be really anything that you might want
they could be objects they could be in
my case strings or numbers or whatever
it is that that you might want now your
lists are always going to be mutable
meaning that you can change them now we
can create a list in one of two ways we
can do it the way that I have up at the
very top there which is by
pre-populating it or we can start off
with an empty list and then we can add
in items by using a few different
methods one of them being append which
sort of as the name implies is going to
put items at the end so if I ran this
code what I would wind up seeing for my
names and my scores would be Christopher
and Susan and then 98 and 99 you'll also
notice that if I want to access an
individual item then I can use the
indexer counting starts with 0 so the
index of 1 is going to give me the
second item the second item in this case
is going to be 99 so if I ran this this
would be the output Python also supports
arrays which are specifically going to
be collections of number data types
now unlike a list in order for me to use
an array I actually have to create an
array object and I have to go get that
by bringing it in from the array library
and that's what that first line of code
right up at the very top here is doing
now we've got a later module where we'll
talk about working with well modules and
libraries and packages and and things
like that so for right now you can take
this hat amount to magic but basically
what we're doing here is we're just
importing in an array now we indicate
the numeric data type that we're going
to be using in my case I'm going to be
using a double and then we're going to
append on our items so we'll add on 97
and 98 we'll print it out and then just
like with a list then we're going to
access the second item here by
indicating one as our index er again all
counting starts with zero so that's why
we're gonna use one to access the second
item and you'll notice the the results
there now one thing that you might be
wondering is hey Christopher what then
is the difference between an array and a
list and well I'm glad you asked uh an
array again has to be numeric data types
and everything inside of there always
has to be the same data type and so that
can give you a little bit of extra
structure and help you avoid a class of
bugs because it's going to prevent you
from accidentally let's say putting in a
string where you were just dealing with
with numbers on the flip side and a list
or sorry a list can actually store any
data type I can be mixed data types etc
so our list is just going to give us
that much more flexibility and again
depending on the type of programming
that you're going to be doing you you'll
find yourself using one data type more
frequently than than the other in my
case especially because of the fact that
I do a lot of what
dev I find myself using lists much more
frequently than I use arrays I also want
to real quick mention the fact that
there are other libraries that are out
there such as numpy that will give you
some additional support when you're
dealing with a large collections of
items now let's talk a little bit about
some common operations that we can
perform on say a list so if we have our
names here and again I've thrown Susan
and Christopher into there then what we
can do is we can access the total number
of items that are in there
by using lend this is different from a
lot of other programming languages where
you'll typically notice that length or
count will be available as a property
here we use a built-in function which
will then be able to pass an array or a
list into and then it will give us back
the total length of items that are
inside of there so in this case it will
give us two because we just have Susan
and Christopher then we can go ahead and
call insert with insert we specify the
index that we want to put this in front
of so because of the fact that we're
specifying 0 it's gonna wind up going at
the very beginning so we'll go ahead
print that out and what won't wind up
noticing there is we'll get bill Susan
and Christopher last but not least is
we've got sort and what sort we'll do is
with strings put it into alphabetical
order in the case of numbers it will put
it into 0 through you know infinity if
you will smallest to the biggest is the
easiest way to put that and then we can
go ahead and print that out which also
then in turn implies the fact that this
is going to have a side-effect so when I
call sort here it's actually going to be
modifying the list you do want to be
careful about that because that might
not be what you're looking for so so do
be careful about that until we'd see the
output down with
bottom there I also want to mention the
fact that we have the ability to grab a
range or grab a smaller subcollection if
you will of our larger collection so if
we take a look at Susan Christopher bill
and Justin I can grab a range by
indicating the start and the end index
here now it's going to be up to but not
including that end index and one way
that we can think about what's going on
behind the scenes is that our indexes
are right here on the commas and on the
square brackets so we would have if you
will behind the scenes 0 1 2 3 & 4 at
the beginning comma comma comma and then
at the very end there with our odd
number 4 so when I say 1 2 3 then what
we're gonna wind up getting here is
starting from the number 1 all the way
up to the number 3 but it's gonna stop
right there so that sub range that I
would wind up getting here is just going
to be Christopher and Bill and that
would wind up being the the output I
also want to mention are real briefly
the fact that let me just change this
back to to read that if I got rid of the
1 so if I just simply said square
bracket colon and then the number 3 then
what that's going to do is it's actually
gonna give me all the way up to but not
including 3 so it's a really kind of
neat bit of shortcut syntax that I can
use to say give me all the way up to but
not including that that number okay now
lists and arrays are great because it
just gives me the ability to take some
items put it somewhere and then I can go
in and access them via a numeric index
but what happens if maybe I want to use
things with key value pairs well that's
what a dictionary is is all about is it
gives me the ability to
I put together groups of items but
instead of doing it based on numeric
indexes I can instead use key value
pairs so the way that we create a
dictionary is very similar to how we
would use say Jason inside of JavaScript
for those of you that are familiar with
Jason is we've got a curly brace and a
curly brace at the very beginning there
and then what we've got is our key value
pair here so our key is first and then
our value is Christopher and then you'll
notice here on on the index that we're
going to grab that index of last and
then we're going to set that to be a
name of Harrison and so that's a very
easy way that we can add on brand new
items onto this dictionary and so now
you'll notice that we could actually
print out the full item or we could
print out items that we access by again
that index instead of using a number
we're just going to use the name so it's
our key value pairs
now having access to a dictionary very
common across a lot of different
programming environments so we can see
this in a lot of different places and so
we'll break down the differences here
and the basic decision point on whether
or not to use a dictionary versus say
using a list is really whether or not I
want to be able to name things and
whether or not I need things to be in a
guaranteed order as of right now only a
list is going to give you a guaranteed
order a dictionary again as of right now
is not going to guarantee you the order
that that does change with some
different libraries that will give you
some different types of dictionaries
that will be able to store the order
there's also the possibility that in the
future it will guarantee the order but
again as of right now
it's not going to guarantee that order
so do keep that in mind when you're
using your your your dictionary that
that storage order is not going to be
guaranteed okay let's get in and take a
look at a demo of all of this