SYNTAX:
list.append(element)
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.append('Python')
print(courseList)
['C', 'Java', 'PHP']
['C', 'Java', 'PHP', 'Python']
SYNTAX:
list.clear()
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.clear()
print(courseList)
['C', 'Java', 'PHP']
[]
SYNTAX:
list.copy()
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
dupListcourseList.copy()
print(dupList)
['C', 'Java', 'PHP']
['C', 'Java', 'PHP']
SYNTAX:
list.count()
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.count('C'))
['C', 'Java', 'PHP']
1
SYNTAX:
list.extend(iterable)
iterable - list, set, tuple, etc.
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.extend(['Python', 'Ruby', 'Perl'])
print(courseList)
['C', 'Java', 'PHP']
['C', 'Java', 'PHP', 'Python', 'Ruby', 'Perl']
SYNTAX:
list.index(element)
Example1:
courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.index('PHP'))
['C', 'Java', 'PHP']
2
courseList = ['C', 'Java', 'PHP']
print(courseList)
print(courseList.index('Python'))
['C', 'Java', 'PHP']
ValueError: 'Python' is not in list
SYNTAX:
list.insert(position,element)
Example:
courseList = ['C', 'Java', 'PHP']
print(courseList)
courseList.insert(2,'Python')
print(courseList)
['C', 'Java', 'PHP']
['C', 'Java', 'Python', 'PHP']
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)
Before pop(): ['C', 'Java', 'PHP']
Popped element: PHP
After pop(): ['C', 'Java']
courseList = ['C', 'Java', 'PHP']
print("Before pop():",courseList)
print("Popped element:",courseList.pop(1))
print("After pop():",courseList)
Before pop(): ['C', 'Java', 'PHP']
Popped element: Java
After pop(): ['C', 'PHP']
SYNTAX:
list.remove(element)
Example1:
courseList = ['C', 'Java', 'PHP']
print("Before remove():",courseList)
courseList.remove('PHP')
print("After remove():",courseList)
Before remove(): ['C', 'Java', 'PHP']
After remove(): ['C', 'Java']
courseList = ['C', 'Java', 'PHP']
print("Before remove():",courseList)
courseList.remove('Python')
print("After remove():",courseList)
Before pop(): ['C', 'Java', 'PHP']
ValueError: 'Python' not in list.
SYNTAX:
list.reverse()
Example:
courseList = ['C', 'Java', 'PHP']
print("Before reverse():",courseList)
courseList.reverse()
print("After reverse():",courseList)
Before reverse(): ['C', 'Java', 'PHP']
After reverse(): ['PHP', 'Java', 'C']
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)
Before sort(): ['PHP', 'C', 'Java', 'Python']
After sort(): ['C', 'Java', 'PHP', 'Python']
courseList = ['PHP', 'C', 'Java', 'Python']
print("Before sort():",courseList)
courseList.sort(reverse=True)
print("After sort():",courseList)
Before sort(): ['PHP', 'C', 'Java', 'Python']
After sort(): ['Python', 'PHP', 'Java', 'C']