In this tutorial, you will learn in-depth about Python list, from the creation of Python list to slicing them, modifying them and different kind of operations that can be performed on them.
In simple language, List in Python is a data type.
If we go one step beyond and define, then:
Python list is an ordered sequence of items separated by a comma ‘,’ and enclosed within square brackets ‘[ ]’.
Python list is like an array. The difference is that array contains ordered sequence of items of similar data types, whereas list contains ordered sequence of items that can be of different data types.
Python list is mutable, which means that the value of items can be altered by accessing it using operator [ ]
.
As mentioned in the definition above, creating a Python list is as simple as putting comma separated values or items inside square brackets '[ ]'
.
The comma separated items or values inside square brackets can be of any type, not necessarily of the same kind.
#creating an empty list
py_list = []
#creating list of integer values
py_list = [1,2,3,4,5]
#creating list of string values
py_list = ['Apple','Banana','Watermelon']
#creating list of different data types
py_list = [1,2,'Hello',3.5]
#creating list of list
py_list = [[1,2],['a','b','c']]
Multiple Python lists can be concatenated using '+'
operator.
#concatenating two python lists
py_list1 = ['a','b','c']
py_list2 = ['d','e','f']
py_list = py_list1 + py_list2
print (py_list) #this will print ['a','b','c','d','e','f']
Once we have created a Python list, we need to access to the members of that list for the further operations. For this, Python has indexing operator '[ ]'
. Index starts from 0.
For example, if we have a list named py_list
, we can access its 5th element by using py_list[4]
as the indexing starts from 0.
There are two ways we can index list in Python:
For example, consider following list with 4 members.
py_list = ['a','b','c','d']
Now, if we index with positive integers using the operator '[ ]'
:
py_list[0] = a
, py_list[1] = b
, py_list[2] = c
, py_list[3] = d
And if we index with negative integers, then
py_list[-1] = d
, py_list[-2] = c
, py_list[-3] = b
, py_list[-4] = a
[adsense1]
IndexError: list index out of range
. And also using floating point numbers instead of integers to the index will also raise an error saying: TypeError
We can insert an item in Python list by using insert
function.
Here is the syntax of insert
function.
list_name.insert(position,value)
Where,
py_list = [1,2,3,4]
#to insert an item to 3rd postion
py_list.insert(2,0)
print (py_list) #output will be: [1,2,0,3,4]
There are two functions to add items to the end of the list.
py_list = [1,2,3]
#to add one item
py_list.append(4) #now list will be [1,2,3,4]
#to add multiple items
py_list.extend([5,6]) #now the list will be [1,2,3,4,5,6]
We can modify the elements in the list by using their index and the assignment operator (=)
. We can change both single element or the range of elements in the list.
py_list = [1,2,3,4]
#to change single element
py_list[0] = 9 #the list will be now [9,2,3,4]
#to change range of elements
py_list[2:3] = [6,7] #this will modify list as [9,2,6,7,4]
There are few methods to remove items from the list. They are listed below.
item_name
from the listlist_name
py_list = [1,2,3,4,5,6]
#to remove single item
py_list.remove(1) #the list will be now [2,3,4,5,6]
py_list.pop(0) #removes item at 0 position,so list becomes [3,4,5,6]]
de py_list[1] #removes item at 1 position,sp list becomes [3,5,6]
#to remove all items
py_list.clear() #now list becomes empty []
#to delete the list
del py_list #this deletes the list and now we can't access py_list
To count the appearance of an item in the list, Python has an inbuilt function list_name.count(item_name)
, which returns the number of times the item appears in the list.
py_list = [1,2,3,4,1,1,6,7,8]
#to count the appearance of 1
py_list.count(1) #this will return 3
To reverse the order of items in the list, Python has an inbuilt function list_name.reverse( )
, which reverses the order of appearance of items in the list.
py_list = [1,2,3,4,5]
#to reverse the order of appearance of items
py_list.reverse() #this will alter the list as [5,4,3,2,1]
We can find the index of a particular item in the Python list by using the following function.
Where item_name
is the name of the item whose index is to be found.
This function will return the index of the first appearance of that particular item in the list.
py_list = [1,2,3,4,5,3,6]
#to find the index of 3
py_list.index(3) #this will return 2 as 2 is the index of 3's first appearance
Python has built in function max( )
and min( )
to find the maximun and minimum item from any sequence.
py_list = [1,2,3,4,1,1,6,7,8]
#to find the maximum and the minimum item from the list
max(py_list) #this will return 8
min(py_list) #this will return 1