Page 137 - Code_GPT_Class_8
P. 137
The following table describes the various built-in methods:
Method Explanation
append( ) Adds the element at the end of the existing list
extend( ) Adds more than one element (in sequential order or elements of the list) to the end of the existing list
insert(x, a) Inserts element ‘a’ at the ‘x’ location of the list
remove(a) Removes the first occurrence of 'a' from the list
index(a) Returns the index of the first occurrence of 'a' from the list
count(a) Returns the count of occurrence (number of times) of a particular element (a) in the list
pop(i) Removes and returns item at the ‘i’ location in the list
copy( ) Returns a copy of the list
clear( ) Removes all the elements from the list and returns an empty list
sort( ) Sorts all the elements in the list
reverse( ) Reverses the order of the elements in the list
Program 8: To use the built-in methods on the list
Program8.py
File Edit Format Run Options Window Help
list1=[13, 25, 41, 63, 82]
list1.append(19)
print(list1)
list1.extend([26, 43, 55])
print(list1)
list1.insert(1,302)
print(list1)
list1.remove(63)
print(list1)
print(list1.index(13))
print(list1.count(41))
list1.reverse()
print(list1)
list1.sort()
print(list1)
On running the above program, you will get the following output:
Output
[13, 25, 41, 63, 82, 19]
[13, 25, 41, 63, 82, 19, 26, 43, 55]
[13, 302, 25, 41, 63, 82, 19, 26, 43, 55]
[13, 302, 25, 41, 82, 19, 26, 43, 55]
0
1
[55, 43, 26, 19, 82, 41, 25, 302, 13]
[13, 19, 25, 26, 41, 43, 55, 82, 302]
List in Python 135

