Python Tutorial
About Lesson

In this Python Tutorial we are going to learn about Python List, so first of all let’s talk about list, List is a collection which is ordered and changeable, or a List is a collection of items in  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.



These are some key points to keep in mind about lists in Python:

  • To create list, you can use square brackets to enclose the elements, separated by commas.
  • Lists are indexed, meaning that you can access individual elements using their index starting from 0.
  • Lists are mutable, which means that you can add, remove or modify elements after the list has been created.
  • Lists can contain elements of any data type, including other lists.
  • You can use built in functions and methods to perform various operations on lists such as sorting, reversing or adding elements.

Lists are very useful for storing and working with collections of data in flexible and dynamic way. they allows you to easily add or remove elements, and they provide different built in functionality for working with and manipulating lists. By understanding how lists work and how to use them effectively, you can take advantage of their strengths and improve the quality and efficiency of your Python code.



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.