#PYTHON(DAY7)
Methods in Python
Python List Methods has multiple methods to work with Python lists
We use dir() function to print the built-in list methods
print(dir(list))
And the expected output is:
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '_delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
append()
- append is used to add another element to the list and this element will be added at the end
x = [1,2,3,4]
print(x, id(x))
print()
x.append(5)
print(x,id(x))
Note: we can not append 2 elements into the list at a time
so, from the above code we get the error as list.append() takes exactly one value
extend()
- extend is used to add two or more than two elements to end of the list
- these values/elements should be passed in container
x = [1,2,3,4]
print(x)
x.extend([5,6])
print(x)
And the expected output for the above code is: [1,2,3,4,[5,6]]
count()
- count will return the frequency of given element
x = [1,2,3,4,6]
print(x)
print(x.count(1))
print(x.count(5))
And the expected output is:
[1,2,3,4,6]
1
0
insert()
- insert as name suggests, is going to insert a new element at the specified index position
- index takes two values i.e index and value
x = [1,2,4]
print(x)
x.insert(3,1)
print(x)
And the expected output is:
[1,2,4,3]
remove()
- remove() is used to remove the specified element
- it removes the first occurence
x = [4,1,2,3,4,4]
x.remove(4)
print(x)
And the expected output is: here the first occurence is removed
[1,2,3,4,4]
pop()
- pop removes the element at given index position, if no index value is passed, the last element will popped out
- the discard/ejected element can be captured/assigned to a new for further use
x = [1,2,3,4,5]
x.pop(3)
print(x)
And the expected output is:
[1,2,3,5]
Note: as the element is popped out, the index values changes
so, from the below code we get the error as pop index out of range
x = [1,2,3,5]
x.pop(4)
print(x)
However, the remove() works on the elements and pop() works on indexes of elements
reverse()
- it reverses the list
x = [1,2,3]
print(x)
x.reverse()
print(x)
And the expected output is:
[1,2,3]
[3,2,1]
sort()
- sorting in ascending or descending
a = list(range(1,11))
b = [4.5,3.8,44.23,2,8,-6.9]
print(a,b, sep = '\n')
And the expected output is:
[1,2,3,4,5,6,7,8,9,10]
[4.5,3.8,44.23,2,8,-6.9]
print(a)
a.sort()
print(a) # ascending
a.sort(reverse = True) # decending
print(a)
And the expected output is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
copy()
- copy is used to store the list in different location to stor it in the different location
e = [1,2,3]
f = e
print(e,f,sep = '\n')
print(id(e), id(f))
e.insert(0,11)
print(e, f, sep ='\n')
print(id(e), id(f))
[1, 2, 3]
[1, 2, 3]
2590467336704 2590467336704
[11, 1, 2, 3]
[11, 1, 2, 3]
2590467336704 2590467336704
clear()
- it is going to erase all the elements from the list and returns an empty list
f = [1,2,3,4]
f.clear()
print(f)
And the expected output is: [ ] (empty list)