Today we going to learn about Python Lists. Earlier we learnt about Python Numbers which can be found here.
Table of Contents
What is a list in Python?
A list is a versatile datatype available in Python. Basically, a list is consists of comma-separated values which are called list items. A list is declared within square brackets. Interestingly it’s not necessary for items in a list to be of the same types.
1. How to create a list in Python? [Syntax]
Let’s go over some of examples of declaring lists below.
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
# a list of integers
int_list=[1,2,3,4,5]
#a list of mixed type of items
mixed_list=['this', 1, 'is', 2, 'a', 3, 'mixed',4, 'list',5]
# to print the lists
print(empty_list)
print(str_list)
print(int_list)
print(mixed_list)
The above code will produce the following output.
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleDeclare.png)
2. How to select list elements by their index?
Each item of a list is assigned an index. The first index is zero, the second index is one, and so forth.
To access items in a list, we can use these index number within a square bracket. For example;
#a list of strings
str_list=['this', 'is', 'a', 'list']
#to access first item
print(str_list[0])
#to access second item
print(str_list[1])
#to access 4th element
print(str_list[3])
The above code will produce output like below.
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleItemAccess.png)
3. Negative Indexes with Lists [List index -1]
The surprising fact is that the index can be negative. It means to read not from the left rather from the right of the list.
#a list of strings
str_list=['this', 'is', 'a', 'list']
#third item from left
print(str_list[2])
#third item from right
print(str_list[-3])
The output of the above code will be like below-
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleItemAccessNegativeIndexing.png)
4. Update/Modify an item in the list
We can update one or more item of a list simply through the index of that item.
#a list of strings
str_list=['this', 'is', 'a', 'list']
print("before updating the list: ")
print(str_list)
str_list[3]='updated list'
print("after updating the list: ")
print(str_list)
The output will be like below.
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleUpdate.png)
5. Deleting an item from a list
To delete an item in a list, there are several methods. Look at the following example to explore it further.
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
#to remove a specific element, like 'is'
str_list.remove('is')
print(str_list)
#to remove an item of a specific index like 2
del str_list[2]
print(str_list)
#there are yet another way to remove an item of a specific index
str_list.pop(0)
print(str_list)
The above code will produce output like below.
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleDelete.png)
Some built-in functions for Python lists
There are some built-in functions to manipulate lists in Python. Let’s look at the following example for understanding.
#an empty list
empty_list=[]
#a list of strings
str_list=['this', 'is', 'a', 'list']
# add an element to the end of the list
str_list.append('appended')
print(str_list)
#insert an item at the defined index
str_list.insert(3,'inserted')
print(str_list)
#to get the index of the first matched item
print(str_list.index('a'))
#to count number of a specific element in a list
print(str_list.count('is'))
#to reverse the order of a list
str_list.reverse()
print(str_list)
#to sort the list in ascending order
str_list.sort()
print(str_list)
The output of the above code will be as follows.
![Python List [Explained with Examples] Python List [Explained with Examples]](https://cdn.pythonlinux.com/2022/05/listExampleBuiltinFunctions.png)
So this is all about python lists for now. Make sure to run every piece of code on you own. Feel free to leave a comment if you have any doubt.
#happy_coding 🙂
Reference: https://docs.python.org/3.6/tutorial/datastructures.html