Welcome back.
In this video we're going to learn about lists.
A list is an ordered collection of items.
You can add, remove and access items in it.
In order to create a list.
Create a new variable equals sign and use this angled bracket.
Inside this angled bracket, place the items you want to store inside it, separated by comma.
So for example, here we have a list called fruits.
This fruits list contains apple, banana, and cherry items.
In order to print this list, we use print and name the list run.
And here we go!
Apple.
Banana.
Cherry.
In order to access items by index, we start by starting from zero, one, and two.
The first fruit, which is apple.
We use the index zero.
If we need to access the last fruit, we need to use negative one.
Negative index equals from end.
For example let me access the second fruit which is fruits one.
Here I am specifying the index.
The second fruit is banana.
Let me access the third index.
So print third fruit.
And you notice that the fruits.
And here I'm specifying the index inside the angled bracket run again.
The third fruit is cherry.
So in arrays and in lists the starting index is zero and not one.
This is a very important thing okay.
In order to get the length of the array or a list, we start with print length of list and we use the
Len function run again and the length of list is equal to three because we can because the fruits array
contains three elements apple, banana, and cherry.
The length of an array is always one more than the highest array index.
It's very simple guys.
We created this list containing three elements.
Apple.
Banana.
Cherry.
printing the.
And the list.
Uh.
Accessing the list and accessing the items of the list by index and getting the length of a list.
Now, if we need to modify, add or remove the items fruits at index one, change the item at index
one.
Index one.
This is zero.
This is one I need to change banana to orange.
So let me run again.
And here the first output is apple orange and the charity banana is replaced by orange.
Here I'm accessing the element.
I am accessing the element by specifying the name of the array.
And inside the angled bracket we specify the index and you change it by using the equals sign.
If we need to append.
If we need to add at the end, we need to add date for example.
So apple, orange, cherry and date.
If we need to remove we use fruits dot Remove and remove the orange by the value, or you can remove
it by the index, but I prefer using by the value.
So Apple, charity and date orange is removed.
So in this way we can access, change and append and remove elements inside a list.
Also, we can loop through a list by specifying for every fruit in fruits, go and print the fruit,
fruit, apple, fruit, cherry and fruit.
Date.
This is after the modification because the last updated list is this apple, cherry and date for every
item.
For every fruit inside this fruits element and this fruits array, go and print all the elements and
print fruit and specify the value of the element Apple, cherry and date okay so this is the basic creation
of lists.
How to access, how to modify, append and remove elements, and how to loop through a list in Python.