List Methods

append() Method: Appends an element at the end of list.
SYNTAX:

list.append(element)

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.append('Python')
print(courseList)

Output:

['C', 'Java', 'PHP']
['C', 'Java', 'PHP', 'Python']

clear() Method: Removes all the elements from the list.
SYNTAX:

list.clear()

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.clear()
print(courseList)

Output:

['C', 'Java', 'PHP']
[]

copy() Method: Returns a copy of list.
SYNTAX:

list.copy()

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
dupListcourseList.copy()
print(dupList)

Output:

['C', 'Java', 'PHP']
['C', 'Java', 'PHP']

count() Method: Returns number that an element occurred in the list.
SYNTAX:

list.count()

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.count('C'))

Output:

['C', 'Java', 'PHP']
1

extend() Method: Adds a list of elements or any iterable to the end of current list.
SYNTAX:

list.extend(iterable)

iterable - list, set, tuple, etc.

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.extend(['Python', 'Ruby', 'Perl'])
print(courseList)

Output:

['C', 'Java', 'PHP']
['C', 'Java', 'PHP', 'Python', 'Ruby', 'Perl']

index() Method: Returns the position of the first occurrence of an element if found. If the element does not find it raises an exception.
SYNTAX:

list.index(element)

Example1:

courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.index('PHP'))

Output:

['C', 'Java', 'PHP']
2

Example2:

courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.index('Python'))

Output:

['C', 'Java', 'PHP']
ValueError: 'Python' is not in list

insert() Method: Adds an element into list at specified position.
SYNTAX:

list.insert(position,element)

Example:

courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.insert(2,'Python')
print(courseList)

Output:

['C', 'Java', 'PHP']
['C', 'Java', 'Python', 'PHP']

pop() Method: Removes an element at specified position.
SYNTAX:

list.pop(position)

Parameter:
position - Optional. A number that specifies the position of the element that you want to be remove. Default value is -1 which means it removes last element.
Example1:

courseList = ['C', 'Java', 'PHP']
print("Before pop():",courseList)
print("Popped element:",courseList.pop())
print("After pop():",courseList)

Output1:

Before pop(): ['C', 'Java', 'PHP']
Popped element: PHP
After pop(): ['C', 'Java']

Example2:

courseList = ['C', 'Java', 'PHP']
print("Before pop():",courseList)
print("Popped element:",courseList.pop(1))
print("After pop():",courseList)

Output2:

Before pop(): ['C', 'Java', 'PHP']
Popped element: Java
After pop(): ['C', 'PHP']

remove() Method: Removes the first occurence of the specified element.
SYNTAX:

list.remove(element)

Example1:

courseList = ['C', 'Java', 'PHP']
print("Before remove():",courseList)
courseList.remove('PHP')
print("After remove():",courseList)

Output1:

Before remove(): ['C', 'Java', 'PHP']
After remove(): ['C', 'Java']

Example2: If the element not in the list then it raises an exception.

courseList = ['C', 'Java', 'PHP']
print("Before remove():",courseList)
courseList.remove('Python')
print("After remove():",courseList)

Output2:

Before pop(): ['C', 'Java', 'PHP']
ValueError: 'Python' not in list.

reverse() Method: Reverses the list.
SYNTAX:

list.reverse()

Example:

courseList = ['C', 'Java', 'PHP']
print("Before reverse():",courseList)
courseList.reverse()
print("After reverse():",courseList)

Output:

Before reverse(): ['C', 'Java', 'PHP']
After reverse(): ['PHP', 'Java', 'C']

sort() Method: Arranges the element in sorted order.
SYNTAX:

list.sort([reverse=BooleanValue])

sort() method have optional argument reverse whose default value is False which sort the elements in ascending order. For descending order assign value True to reverse argument.

Example1:

courseList = ['PHP', 'C', 'Java', 'Python']
print("Before sort():",courseList)
courseList.sort()
print("After sort():",courseList)

Output1:

Before sort(): ['PHP', 'C', 'Java', 'Python']
After sort(): ['C', 'Java', 'PHP', 'Python']

Example2:

courseList = ['PHP', 'C', 'Java', 'Python']
print("Before sort():",courseList)
courseList.sort(reverse=True)
print("After sort():",courseList)

Output2:

Before sort(): ['PHP', 'C', 'Java', 'Python']
After sort(): ['Python', 'PHP', 'Java', 'C']