Array/List Methods in Python
Shubham Kumar Gupta
Posted on February 15, 2023
Array/List Methods in Python
1. append(x):
Adds an element to the end of the list
syntax: list.append(element)
l = [7, 6, 7, 5, 4, 9]
l.append(5)
print(l)
output: [7, 6, 7, 5, 4, 9, 5]
2. insert(i, x):
Insert an element at a given index.
syntax: list.insert(index, element)
l = [7, 6, 7, 5, 4, 9, 5]
l.insert(1, 3)
print(l)
output: [7, 3, 6, 7, 5, 4, 9, 5]
3. remove(x):
Removes the first occurrence of the given element.
syntax: list.remove(element)
l = [7, 3, 6, 7, 5, 4, 9, 5]
l.remove(5)
print(l)
output: [7, 3, 6, 7, 4, 9, 5]
4. pop(i):
Removes the element from the given index. If no index is given, it removes the last element from the list.
syntax: list.pop(index)
l = [7, 3, 6, 7, 4, 9, 5]
l.pop(2)
print(l)
l.pop()
print(l)
output: [7, 3, 7, 4, 9, 5], [7, 3, 7, 4, 9]
5. index(x, s, e):
Returns the list's first index of a given element. Start and end parameters are optional and specify the index to start the search from and, end to. If only one parameter other than the element is given, it defaults to start. If element is not found in the list, it returns a ValueError.
syntax: list.index(element, start (optional), end (optional))
l = [7, 3, 7, 4, 9, 7]
x = l.index(7)
print(x)
y = l.index(7, 2)
print(y)
z = l.index(7, 3, 6)
print(z)
p = l.index(5)
print(p)
output: 0 , 2 , 5 , ValueError
6. count(x):
Counts the number of times an element appears in the list
syntax: list.count(element)
l = [7, 3, 7, 4, 9]
x = l.count(7)
print(x)
output: 2
7. sort(*, key = , reverse = True/False):
Sorts the list in ascending (by-default), descending, alphabetical (by-default), reverse-alphabetical, or according to a user-given order.
syntax: list.sort(*, key = , reverse = True/False(Default))
l1 = [7, 3, 7, 4, 9]
l1.sort()
print(l1)
l1.sort(reverse = True)
print(l1)
l2 = ['bb', 'a', 'dddd', 'ccc']
l2.sort()
print(l2)
l2.sort(reverse=True)
print(l2)
l2.sort(key = len)
print(l2)
l2.sort(key = len, reverse = True)
print(l2)
output: [3, 4, 7, 7, 9], [9, 7, 7, 4, 3], ['a', 'bb', 'ccc', 'dddd'], ['dddd', 'ccc', 'bb', 'a'], ['a', 'bb', 'ccc', 'dddd'], ['dddd', 'ccc', 'bb', 'a']
8. reverse():
Reverses the elements of the given list.
syntax: list.reverse()
l = [7, 3, 7, 4, 9]
l.reverse()
print(l)
output: [9, 4, 7, 3, 7]
9. copy():
Returns a copy of the given list.
syntax: list2 = list1.copy()
l1 = [9, 4, 7, 3, 7]
l2 = l1.copy()
print(l1)
output: [9, 4, 7, 3, 7]
10. extend(list):
Adds elements of the second list to the end of the first list.
syntax: list1.extend(list2)
l1 = [9, 4, 7, 3, 7]
l2 = ['bb', 'a', 'dddd', 'ccc']
l1.extend(l2)
print(l1)
output: [9, 4, 7, 3, 7, 'bb', 'a', 'dddd', 'ccc']
11. clear():
Removes all the elements of the given list.
syntax: list.clear()
l1 = [9, 4, 7, 3, 7, 'bb', 'a', 'dddd', 'ccc']
l1.clear()
print(l1)
output: []
REFERENCES
Posted on February 15, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024