This page will specifically cover available built-in Python functions which helps in performing variety of operations(such as adding, modifying & deleting items) on list objects. Please visit this page to understand List in detail.
Syntax: len(list_object)
a_list = [1, 2, 3, 6, 'A']
len(a_list)
Output: 5
a_list = []
len(a_list)
Output: 0
Syntax: list_object.append(value)
a_list = [1, 2, 3, 6, 'A']
a_list.append('C')
print(a_list)
Output: [1, 2, 3, 6, 'A', 'C']
a_list = [1, 2, 3, 6, 'A']
a_list.append(['C','D'])
print(a_list)
Output: [1, 2, 3, 6, 'A', ['C', 'D']]
Visit this page to understand difference between extend and append functions
Syntax: list_object.extend(object)
a_list = [1, 2, 3, 6, 'A']
a_list.extend(4)
print(a_list)
Output: TypeError: 'int' object is not iterable
a_list = [1, 2, 3, 6, 'A']
a_list.extend(['C','D'])
print(a_list)
Output: [1, 2, 3, 6, 'A', 'C', 'D']
a_list = [1, 2, 3, 6, 'A']
a_list.extend('DBMS')
print(a_list)
Output: [1, 2, 3, 6, 'A', 'D', 'B', 'M', 'S']
Visit this page to understand difference between extend and append functions
Syntax: list_object.insert(index_position, value)
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.insert(2,'X')
print(a_list)
Output: ['a', 'b', 'X', 'f', 'h', 'j']
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.insert(-1,'Y')
print(a_list)
Output: ['a', 'b', 'f', 'h', 'Y', 'j']
Syntax 1: list_object.index(value)
Syntax 2: list_object.index(value, start=startingNumber)
Syntax 3: list_object.index(value, start=startingNumber, stop=endingNumber)
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.index('c')
Output: ValueError: 'c' is not in list
Solution: Check if value is present before running index function
if 'c' in a_list:
a_list.index('c')
a_list = ['a', 'b', 'f', 'b', 'j','k','l','b','g','b']
a_list.index('b')
Output: 1
a_list = ['a', 'b', 'f', 'b', 'j','k','l','b','g','b']
a_list.index('b', 4)
Output: 7
a_list = ['a', 'b', 'f', 'b', 'j','k','l','b','g','b']
a_list.index('b' , 4, 6)
Output: ValueError: 'b' is not in list
a_list = ['a', 'b', 'f', 'b', 'j','k','l','b','g','b']
a_list.index('b' , 8, 10)
Output: 9
Syntax: list_object.count(value_to_check_occurence)
a_list = [1, 2, 3, 6, 'A', 6]
a_list.count(3)
Output: 1
a_list = [1, 2, 3, 6, 'A', 6]
a_list.count(6)
Output: 2
a_list = [1, 2, 3, 6, 'A', 6]
a_list.count(5)
Output: 0
Syntax 1: list_object.remove(value)
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.remove('d')
Output: ValueError: list.remove(x): x not in list
Solution: Check if value is present before running remove function
if 'd' in a_list:
a_list.remove('d')
a_list = ['a', 'b', 'f', 'h', 'b', 'j']
a_list.remove('b')
print(a_list)
Output: ['a', 'f', 'h', 'b', 'j']
Visit this page to understand difference between remove and pop functions
Syntax 1: list_object.pop(value)
Syntax 2: list_object.pop(value, indexToPop)
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.pop(7)
Output: IndexError: pop index out of range
Solution: Check length of list before running pop function
if len(a_list)>=7:
a_list.pop(7)
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.pop()
Output: 'j'
a_list = ['a', 'b', 'f', 'h', 'j']
a_list.pop(1)
Output: 'b'
Visit this page to understand difference between remove and pop functions
Syntax 1: list_object.sort()
Syntax 2: list_object.sort(reverse=True) # Sort in descending order
Syntax 3: list_object.sort(key=somefunction) # Sort using function
Syntax 4: list_object.sort(reverse=True,key=somefunction) #Sort using function in descending Order
a_list = [1, 2, 3, 6, 'A', 6]
a_list.sort()
Output: TypeError: '<' not supported between instances of 'str' and 'int'
a_list = [1, 4, 3, 7, 6]
a_list.sort()
print(a_list)
Output: [1, 3, 4, 6, 7]
a_list = [1, 4, 3, 7, 6]
a_list.sort(reverse=True)
print(a_list)
Output: [7, 6, 4, 3, 1]
a_list = [1, 4, 3, 7, 6]
def remainder(num):
return num%2
a_list.sort(key=remainder)
print(a_list)
Output: [4, 6, 1, 3, 7]
a_list = [1, 4, 3, 7, 6]
def remainder(num):
return num%3
a_list.sort(reverse=True, key=remainder)
print(a_list)
Output: [1, 4, 7, 3, 6]
Syntax: list_object.copy()
a_list = [1, 2, 3, 6, 'A']
b_list = a_list.copy()
print(a_list)
Output 2: [1, 2, 3, 6, 'A']
b_list.pop() # Removing last item from b_list
Output 3: 'A'
print(b_list) # 'A' value not present in b_list
Output 4: [1, 2, 3, 6]
print(a_list) # 'A' value is still present in a_list
Output 5: [1, 2, 3, 6, 'A']
Syntax: list_object.reverse()
a_list = [1, 2, 3, 6, 'A']
a_list.reverse()
print(a_list)
Output: ['A', 6, 3, 2, 1]
Syntax: list_object.clear()
a_list = [1, 2, 3, 6, 'A']
a_list.clear()
print(a_list)
Output: []