Course Content

  • List

Course Content

Python Lists

Hello guys. Welcome to LearnVern. We saw about String in the last topic.

Today we will work with List and see how it works in Python.

First of all, what is List? List is a collection in Python which stores the data in a sequence.

Plus List allows to store multiple datatype values like String, Integer, Float, Character.

You can even store a duplicate value.

Plus List works on Index Value.

Index value starts from 0. You can easily access any value from the list through Index Value.

List is mutable. Mutable means it is changeable at runtime.

Now...what can be done with List?

We are going to do a lot of things practically in List.

We will see how to Create List, how to Indexing & Slicing, learn how to apply builtin methods as well as how to make Nested List.

I’ll show you numerous ways on how to work with List in Python.

Let’s move onto practicals.

I have opened up Jupyter right now.

I’ll markdown Creating a List.

How to create a list.

I’ll take a new tab here. I’ll make a list here L1.

Making a List is very easy.

List is represented with square brackets here. With what? Square brackets.

I’ll show you by storing each value in every new list.

First of all I’ll store integers.

I'll write in the comment “This list stores the integer values”.

Now I’ll take another list.

This list stores the string values.

I’ll create list 2. I’ll write Apple, Mango, Orange.

Now I will print both of them.

I’ve already told you that List allows all kinds of datatypes.

I’ll write here integer list, L1.

Another list is String list, L2.

You can see both the lists got printed here.

So you can store any datatype value here.

I made 2 separate list to let you know that we can enter string, characters as well as integers. Okay?

Now I want to add duplicate values in my list too.

We will see how to add duplicate values.

I’ll create a new tab and markdown, Duplicate Values in List.

Suppose I took an L1 list. 1, 2, 3, 4, 5, 6. And I’ll write 1, 2 and 3 again.

Will it allow me? Yes, it will definitely allow me.

Duplicate values. It doesn’t matter in List if there are duplicate values as well.

It will allow everything. See, it did allow the duplicate values to print as well.

We will see one more thing List has that is Constructor.

How is List Constructor used?

Suppose I took a list named L1 and I want to use List Constructor, it has a by default constructor.

Now, I took 1 more round bracket & stored Apple, Mango and Orange.

Remember to Note there are double brackets.

We used to take square brackets in List but here we will take 2 round brackets.

Now I’ll print L1 & it gave me a list.

It got converted into a list.

You can see the square brackets in the List Constructor.

So this is how you can use a list constructor.

We will move forward to List Slicing.

We’ve already seen String Slicing before.

Now we will see List Slicing.

Let’s see how it works.

I’ll take a list L1 in which I’ll store 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 values.

This is my current list & I’ll print it.

It got printed.

Now I have to work around Indexing values because the slicing will work only & only through Indexing value.

Access the value of Index 1.

How will we do it?

Now I’ll print it.

Index 1 value. How to use it?

L1 which is our list. Square brackets. Which I already told you regarding Slicing.

1 here. Index 1 & it showed us the value 2.

Moving ahead on the same path.

Access the value between index 1 to 6.

Print Index between 1 to 6. And L1. 1 to 6 so 1:6.

As soon as you run it, the values between 1 to 6 will be printed.

Next I want to Access the value till index 6.

We had selected Between 1 to 6 above & now we are selecting till 1 to 6.

Print Index till 6. L1 and we have to write :6.

It will take values from 0 to 6 and print it.

Now I want to Access the last value from the list.

I want to access the last value from the list. How will I do it?

Print. Last value. L1, square brackets and as you want to take the last one, you have to write -1.

So this is how you can do string slicing as well.

Understood?

Onto the next. Next we will change the items of the list.

I’ll write List Item Change.

How to do List Item Change.

Suppose I have a variable X and I made a list such as Apple, Banana, Cherry, Mango, Orange and lastly Kiwi.

I’ll print my current list now. X.

My current list is in such a way, Apple, Banana, Cherry, Mango, Orange, Kiwi.

Now I want to change something here.

Change the item in List.

X where our list is stored. 1:3. Banana and Mango.

We want to change between Banana & Mango.

Let’s see how to change it.

I’ll take Grapes & Watermelon.

And I’ll print the updated list now. X.

You can see that Banana & Cherry got replaced by Grapes & Watermelon.

The remaining list stayed the same.

Understood how to change the items in the list?

Now we will insert into the list.

So Insert Into List.

We have an Insert method for inserting.

Let’s see how.

I’ll take a list L1 such as 1, 2, 3, 4, 5, 6.

I’ll print Current List L1.

So my current list got printed.

Now in this particular list on Index 2, I want to add Python.

What will I do?

L1 . (dot) insert. Insert method.

We have to pass the Index value. I want to pass it in 2. And what do you want to update?

We have to update Python. Now I’ll print my Updated List. L1.

You can see that Python got stored instead of 3 here.

So this is how you can use Insert into the list.

I hope you understood it clearly.

I’ll write a comment for you here Updating the list using the Insert Method.

Understood how to use the Insert Method?

Now I want to Extend the list. Meaning I want to store 1 particular list into another.

I’ll write down Extend the List.

Let’s see how to do it.

I have a list L1 under which I wrote 1, 2, 3, 4 & 5. 5 values.

I have the 2nd list in the same manner.

L2. A, B, C, D, and E. I stored 5 characters here.

Now I want to extend this L2 list into L1.

What will I do? I’ll simply write L1 (dot) extend. Extend what? L2.

Simply print L1 here. And you can see both the lists got merged into 1.

I’ll write a comment here, Extending the list of L2 into List of L1.

This makes it easier for you to know. Alright.

Further ahead we will see what are the ways to remove the items from the list.

Remove Items from List.

The 1st method we will be using is Remove.

Which method? Remove method.

This method is used to remove elements. Okay? This method does not remove indexes, it removes the elements from lists.

Suppose I have a list L1, under which I wrote 1, 2, 3, 4, 5, 6, 7.

I stored 7 values in this list.

Now we have to remove it.

Firstly, I’ll print this current list so we get an idea as to what our current list is.

Like I printed L1 here.

This is my current list with 7 values.

Now I’ll write removing the item using the Remove Method.

I have to remove from L1. Suppose I have to remove 3, I’ll write only 3 here.

Now I'll print my updated list. Let’s see what happens.

You can see that 3 got removed from the updated list.

So this is how Remove works in the Remove list.

Let’s move forward.

We shall see how the Pop method is used.

This method is used to remove the last value from the list.

I’ll copy paste the L1 list from above.

Now I’ll print my current list here. L1.

Now we have to remove.

So remove item using the pop method.

Which method? Pop method.

L1 . (dot) pop.

And now I'll directly print the list.

Updated list. See, the 7 is not printed here.

It popped the last item, meaning it removed the last item.

Next we will use the Del Method. Which method? Del Method.

This method is used to remove items from the index.

Okay? I’ll use List 1 again.

Print it out as well. Current list. L1.

Now I want to remove an item by using the Del method.

I’ll write here Remove item using the Del method.

Write Del which is our Del method. The name of our list. L1.

And a particular Index value. The one we want to remove.

I’ll write 2 here. And now print my updated list.

What is our updated list? 1, 2, it removed 3 from here.

So this is how you can use the Del method in List.

There is another method called Clear method.

Clear method.

So this method is used to clear the list.

Clear method will remove your whole list.

I took list 1. Now I'll print my current list. L1.

Now I’ll Remove item using the Clear method.

You just have to write L1 . (dot) clear.

Now when you’ll print the Updated List, L1.

You can see that the list has disappeared.

I’ve shown you all the ways to use the List in Python.

So what did we see at the very first?

We saw how to create a List, we saw what values can be stored in the list, we saw Duplicate values, we saw how to use List Constructor, List Slicing,

how to change List items by using Index value, we saw how to Insert and Extend.

We also saw how to Remove Items where we covered all the methods.

In the next video, we will see how to work with Tuple in Python.

FAQs

A list is a Python container that is both ordered and modifiable. It is one of the most used data structures in Python. To make a list, put the elements inside square brackets ([]) and separate them with commas.

A list is made in Python by putting entries inside square brackets [] and separating them with commas. There can be any number of entries in a list, and they can be of many types (integer, float, string, etc.). Another list can be included as an item in a list. This is referred to as a nested list.

A Python list may contain different types! Indeed, you can store a number, a string, and even another list within a single list. ... You can also access a range of items in a list by using the slicing operator (colon).

Multiple items can be stored in a single variable using lists. Lists are one of four built-in Python data structures for storing collections of data; the other three are Tuple, Set, and Dictionary, all of which have different properties and applications.

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere