- Arrays is a collections of one or more items at the same time.
- We can access the elements using index values of array.
append()
- It is used to add or append the element to existing array at the end of array list.
Syntax
list.append(element)
Example
a = [1, 2, 3]
b = [7, 8, 9]
b.append(2)
print(b)
# output: [7, 8, 9, [1, 2, 3]]
clear()
- It is used to clear or remove all the elements in the array list.
Syntax
list.clear()
Example
a = ["Hi", "Welcome", "Bye"]
a.clear()
print(a)
# output: []
copy()
- It is used to copy the specific list of data.
Syntax of copy
list.copy()
Example
a = ["ball", "bat", "cat", "dog"]
x = a.copy()
print(x)
# output: ['ball', 'bat', 'cat', 'dog']
count()
- It gives the number of elements for specified value in the list.
Syntax
list.count(value)
Example
n = [1,2,3,4,1,3,6,4,8]
m = n.count(3)
print(m)
# output: 2
extend()
- It adds the specific list of elements at the end of the current array list.
Syntax
list.extend(iterable)
# iterable - it can be a list, set, tuple etc.
Example
n = ['This is a list']
m = ['List of elements.']
m.extend(n)
print(m)
# output: ['This is a list', 'List of elements.']
index()
- It returns the index value of a specific element.
- if we have duplicates in the list, the index will the take the first occurs of the element in the list.
Syntax
list.index(elemnt)
#elemnt - it can be string, number, list etc.
Example
n = [1,2,3,4,1,3,6,4,8]
x = n.index(4)
print(x)
# output: 3
insert()
- It used to insert the element at specific index value in the array. #### Syntax
list.insert(pos, elemnt)
# pos - at which position of index we want to insert.
# elemnt - it can be string, number, object etc.
Example
a = ['ball', 'bat', 'cat', 'dog']
a.insert(1, 'lion']
print(a)
# output: ['ball', 'lion', 'bat', 'cat', 'dog']
pop()
*It removes the element at a specific value.
Syntax
list.pop(pos)
#pos - at which position of index we want.
Example
a = ['ball', 'bat', 'cat', 'dog']
a.pop(1)
print(a)
# output: ['ball', 'cat', 'dog']
remove()
- It removes the first occurrence of the element with specified value. #### Syntax
list.remove(elemnt)
#elemnt - it can be string, number, list etc.
Example
n = [1,2,3,4,1,3,6,4,8]
n.remove(1)
print(n)
# output: [2, 3, 4, 1, 3, 6, 4, 8]
reverse()
- It create a new string containing the original data in reverse order.
Syntax
list.reverse()
Example
a = ['ball', 'bat', 'cat', 'dog']
a.reverse()
print(a)
# output: ['dog', 'cat', 'bat', 'ball']
sort()
- It will sort the list in ascending order by default.
Syntax
list.sort(reverse = true|false, key=myfun)
#reverse - reverse=True will sort the list descending. Default is reverse=False
#key - function to specify the sorting criteria.
Example
n = [1,2,3,4,1,3,6,4,8]
n.sort()
print(n)
# output: [1, 1, 2, 3, 3, 4, 4, 6, 8]
Top comments (0)