Python List

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.

Python list

Python lists: Introduction
Creating a list
Accessing members of the list
Inserting an item at desired position in the list
Adding an item to the end of the list
Modifying the elements in the list
Removing items from the list
Count appearance of an item in the list
Reverse the order of items in the list
Finding index of a particular item in the list
Finding largest and the smallest item in the list

Python List: Introduction


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 [ ].

Creating a Python list

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']

Accessing members of Python list

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:

  • index with positive integers: Index from left starting with 0 and 0 indexing first item in the list
  • index with negative integer: Index  from the right starting with -1 and -1 indexing the last item in the list

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] = apy_list[1] = bpy_list[2] = cpy_list[3] = d

And if we index with negative integers, then

py_list[-1] = dpy_list[-2] = cpy_list[-3] = bpy_list[-4] = a

[adsense1]

Common Programming Errors
Trying to access 5th element in the list of 4 elements will raise an error saying: 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

Inserting an item in a desired position in the list

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,

  • list_name is the name of list in which we want to insert an item
  • position is the index of position where we want to insert
  • value is the element we want to insert
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]

Adding an item to the end of the list

There are two functions to add items to the end of the list.

  • append(): to add an element to the end of the list
  • extend(): to add multiple items at 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]

Modifying the elements in the list

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]

Removing items from the list

There are few methods to remove items from the list. They are listed below.

  • list_name.remove(item_name): Removes the item with item_name from the list
  • list_name.pop(index): Removes item with provided index and returns it
  • del list_name[index]: Removes item with given index
  • del list_name: Deletes the list
  • list_name.clear(): Removes every item from list_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

Count the appearance of an item in the Python 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

Reverse the order of items in the Python list

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]

Find the index of particular item in the list

We can find the index of a particular item in the Python list by using the following function.

  • list_name.index(item_name)

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

Finding largest and smallest items in a Python list

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