DEV Community

CNavya21
CNavya21

Posted on

PYTHON : ARRAY METHODS

  • 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)
Enter fullscreen mode Exit fullscreen mode

Example

a = [1, 2, 3]
b = [7, 8, 9]
b.append(2)
print(b)
# output: [7, 8, 9, [1, 2, 3]]
Enter fullscreen mode Exit fullscreen mode

clear()

  • It is used to clear or remove all the elements in the array list.

Syntax

list.clear()
Enter fullscreen mode Exit fullscreen mode

Example

a = ["Hi", "Welcome", "Bye"]
a.clear()
print(a)
# output: []
Enter fullscreen mode Exit fullscreen mode

copy()

  • It is used to copy the specific list of data.

Syntax of copy

list.copy()
Enter fullscreen mode Exit fullscreen mode

Example

a = ["ball", "bat", "cat", "dog"]
x = a.copy()
print(x)
# output: ['ball', 'bat', 'cat', 'dog']
Enter fullscreen mode Exit fullscreen mode

count()

  • It gives the number of elements for specified value in the list.

Syntax

list.count(value)
Enter fullscreen mode Exit fullscreen mode

Example

n = [1,2,3,4,1,3,6,4,8]
m = n.count(3)
print(m)
# output: 2
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Example

n = ['This is a list']
m = ['List of elements.']
m.extend(n)
print(m)
# output: ['This is a list', 'List of elements.']
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Example

n = [1,2,3,4,1,3,6,4,8]
x = n.index(4)
print(x)
# output: 3
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Example

a = ['ball', 'bat', 'cat', 'dog']
a.insert(1, 'lion']
print(a)
# output: ['ball', 'lion', 'bat', 'cat', 'dog']
Enter fullscreen mode Exit fullscreen mode

pop()

*It removes the element at a specific value.

Syntax

list.pop(pos)
#pos - at which position of index we want.
Enter fullscreen mode Exit fullscreen mode

Example

a = ['ball', 'bat', 'cat', 'dog']
a.pop(1)
print(a)
# output: ['ball', 'cat', 'dog']
Enter fullscreen mode Exit fullscreen mode

remove()

  • It removes the first occurrence of the element with specified value. #### Syntax
list.remove(elemnt)
#elemnt - it can be string, number, list etc.
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

reverse()

  • It create a new string containing the original data in reverse order.

Syntax

list.reverse()
Enter fullscreen mode Exit fullscreen mode

Example

a = ['ball', 'bat', 'cat', 'dog']
a.reverse()
print(a)
# output: ['dog', 'cat', 'bat', 'ball']
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)