Python List Methods


Method Description
append() To add a new item to the end of the list.
clear() To remove all the elements from the list.
copy() Returns a new copy of the list.
count() Count the number of times an item is repeated in the list.
extends() To merge one list to another.
index() Index position of the first appearance of the item.
insert() To insert an element at any particular index position in the list.
len() To find the length of the list.
pop() Remove an item from the list.
remove() Removes the specific item from the list using the value of the item.
reverse() To reverse the elements of the list.
sort() To sort the items inside the list.

Python List append() Method

Python List append() can be used to add a new item to the end of the list.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.append('USA')
emp_details
Out[1]:
['Brad', 'Lee', 'Sales', 380, 250000, True, 'USA']

Python List clear() Method

Python List clear() will remove all the elements from the list.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.clear()
emp_details
Out[1]:
[]

Python List copy() Method

Python List copy() returns a new copy of the list. Though it is not mandatory to use the copy function, we can directly assign the list to another variable and it will create a new list.

With copy() funciton.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_copy = emp_details.copy()
emp_copy
Out[1]:
['Brad', 'Lee', 'Sales', 380, 250000, True]

Without copy() function.

In [2]:
emp_without_copy = emp_details
emp_without_copy
Out[2]:
['Brad', 'Lee', 'Sales', 380, 250000, True]

Python List count() Method

Count the number of times an item is repeated in the list.

In [1]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True,'USA']
emp_details.count('USA')
Out[1]:
2

Python List extends() Method

Extends is used when we have two lists and we want each element of the second list to be merged in the first one. Note: Items we want to merge can be any iterable, not mandatorily list.

In [1]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_address = ['New York','USA']
emp_details.extend(emp_address)
In [2]:
emp_details
Out[2]:
['Brad', 'Lee', 'USA', 'Sales', 380, 250000, True, 'New York', 'USA']

Python List index() Method

Returns the index position of the first appearance of the item from the list. If the item is not found, it returns as an error. It has an optional start and end index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.

Note: Here end index is up to the index position. Item at end index position is not included.

The first index position is returned when the item is found in the list.

In [1]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True,'Sales']
emp_details.index('Sales')
Out[1]:
3

Returns error when the item is not found in the list.

In [2]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Peter')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-c61612f6191e> in <module>()
      1 emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
----> 2 emp_details.index('Peter')

ValueError: 'Peter' is not in list

Using the start and end index positions.

In [3]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Sales',2,4)
Out[3]:
3

When item not found within index range.

In [4]:
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Sales',0,2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d3737c85175c> in <module>()
      1 emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
----> 2 emp_details.index('Sales',0,2)

ValueError: 'Sales' is not in list

Python List insert() Method

To insert an element at any particular index position in the list.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.insert(2,'USA')
emp_details
Out[1]:
['Brad', 'Lee', 'USA', 'Sales', 380, 250000, True]

Python List len() Method

To find the length of the list we use len() function.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
len(emp_details)
Out[1]:
7

Python List pop() Method

It will remove an item from the list. By default it will remove the last item from the list but we do have an option to specify the index. we can save the pop item in the variable.

In this example output is ‘USA’, the item it poped.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
emp_details.pop()
Out[1]:
'USA'

List after pop.

In [2]:
emp_details
Out[2]:
['Brad', 'Lee', 'Sales', 380, 250000, True]

If we want to pop ‘Sales’ from emp_details list, we can specify the index location

In [3]:
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
item_poped = emp_details.pop(2)
item_poped
Out[3]:
'Sales'

List after pop.

In [4]:
emp_details
Out[4]:
['Brad', 'Lee', 380, 250000, True, 'USA']

Python List remove() Method

Removes the specific item from the list using the value of the item. If the item is repeated several times then only the first occurrence is removed.

Note: Only the first occurrence of ‘Sales’ is removed.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True,'Sales']
emp_details.remove('Sales')
emp_details
Out[1]:
['Brad', 'Lee', 380, 250000, True, 'Sales']

Python List reverse() Method

To reverse the elements of the list.

In [1]:
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
emp_details.reverse()
emp_details
Out[1]:
['USA', True, 250000, 380, 'Sales', 'Lee', 'Brad']

Python List sort() Method

To sort the items inside the list.

In [1]:
my_list = ['X','c','C','c','r','a','d','b']
my_list.sort()
my_list
Out[1]:
['C', 'X', 'a', 'b', 'c', 'c', 'd', 'r']