Python Tutorial
About Lesson

In this Python List tutorial we are going to learn about Creating List in Python, so first of all let’s talk about list, A list is a collection which is ordered and changeable, or A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, You can put anything you want into a list. you can use square brackets ([]) for creating list.

 

 

OK now let’s create our first python list. this is a simple list of programming languages, and after that we want to print our list and also print the type of our languages.

 

 

 

Run the code and this is the result.

Python List - Creating List in Python
Python List – Creating List in Python

 

 

If you want to access an specific element in python list, than you need to use the index of that item, list indexing starts from 0.

 

 

Now we are going to learn how you can use individual items from a list.

 

 

 

As i have already said that python list is changeable it means that you can change the items of the list, now let’s change an item in a list. in here we want to change the first index item in our list.

 

 

 

Run the code and this is the example.

How to Create List in Python
How to Create List in Python

 

 

 

Also you can add elements to a list, the simplest way is using append, so when we append an item in list, the new item will be added at the end of the list.

 

 

 

This is the result.

Python List Adding Item
Python List Adding Item

 

 

 

also using append you can create dynamically lists. so first we have created an empty list after that we are appending the items in to list.

 

 

 

using insert() method you can add elements at any position in your list, because when you are going to use insert you can specify the index of the element.

 

 

 

This is the result.

Python List Inserting Data
Python List Inserting Data

 

 

 

There are different ways that you can remove items from a list, you can use del keyword if you know the position or index of the list.

 

 

The pop() method removes the last item in a list, but it lets you work with that item after removing it.

 

 

 

For removing item by value we can use remove() method.

 

 

 

This is the result.

Removing Item in List
Removing Item in List

 

 

 

For sorting the list we can use sort() method.

 

 

You can also sort this list in reverse alphabetical order by adding reverse to True.

 

 

There is another function for soring lists that is called sorted(), using sorted() method you can sort list items temporarily.

 

 

Also you can reverse the order of a list.

 

 

 

Also you can find the length of a list.

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found