Quick Introduction To Python Arrays Method

In this article you will learn about Python arrays method

Python arrays method – Before moving ahead, let’s know a little bit about Introduction to Python Array

Arrays – It is used to hold any number of values in one variable. However, Python doesn’t not built-in support of arrays, but a list can be used as an array. Using an array is quite simple, Python needs to import its library for this.

Python has some set of built-in methods which can be used on array or list.

clear() method – It clears or removes the all items of the list.

Syntax - list.clear()

Parameter Values - No parameter values.

Example 1- Use of clear() method to get an empty list.

x =['ab', 'cd', 'ef', 'gh']
x.clear()                             

print(x)
python-arrays-method

Explanation – As it is shown clearly that it removed all elements of array and returned an empty list.

copy() method – It returns a list of copy of original list.

Syntax - list. copy()

Parameter Values - No parameter values.

Example 2- Use of copy() method to copy a list.

x =['ab', 'cd', 'ef', 'gh']
x.copy() 

print(x)
python-arrays-method

Explanation – As it is shown clearly that it returned the copy of whole existing array.

count() method – It returns the number of specified value.

Syntax - list.count(value)

Parameter Values -

value - It is required argument. The specified value to be counted.

Example 3- It returns the number of ‘a’ present in the list.

x =['a', 'b', 'c', 'a', 'd', 'e', 'a']
z = x.count('a')

print(z)           
python-arrays-method

Explanation – As it is shown clearly that it returned the number of letter ‘a’ appeared in array.

extend() method – It adds the elements of a new list in the original list.

Syntax - list.extend(value)

Parameter Values-

value - It is required argument. The list of elements to be added.

Example 4- It returns original list with including new elements.

x =['ab', 'cd', 'ef', 'gh']
z = ['q', 'w', 'e']
x.extend(z)

print(x)
python-arrays-method

Explanation – As it is shown clearly that it returned a new array after adding a list in it.

insert() method – It adds a specified value at specified index number.

Syntax - list.insert(index number, element)

Parameter Values -

index number - Where to be inserted specified element.

element - The specified value to be inserted.

Example 5- Use of insert() method to insert specified element.

x =['ab', 'cd', 'ef', 'gh']
z = x.insert(1, 'we')

print(z)
python-arrays-method

Explanation – As it is shown clearly that it returned a new array after inserting a string at the specified index number.

reverse() method – It returns the reverse version of original list.

Syntax - list.reverse()

Parameter Values - No parameter values.

Example 6- Use of reverse() method to reverse the list.

x = ['a', 'b', 'c', 'd', 'e']
x.reverse()

print(x)
python-arrays-method

Explanation – As it is shown clearly that it returned array with the reverse order of elements.

sort() method – By default it returns elements of list in ascending order.

Syntax - list.sort(reverse= True|False, key=Func)

Parameter Values -

reverse - It is optional argument. By default it will arrange elements in ascending order otherwise reverse=True will sort the list in descending order.

Key - It is optional argument. A function to be specified the sorting criteria.

Example 7- Use of sort() method to arrange elements of the list by default.

x = ['d', 'a', 'c', 'e', 'b']
x.sort()

print(x)
python-arrays-method

Explanation – As it is shown clearly that it retuned array after arranging elements in ascending order.

Example 8- Use of sort() method to arrange elements of the list in descending order.

x = ['d', 'a', 'c', 'e', 'b']
x.sort(reverse=True)

print(x)
python-arrays-method

Explanation – As it is shown clearly that it retuned array after arranging elements in descending order. Here reverse=True, therefore it returned in descending order otherwise, it would returned it ascending order.

Example  9- Sort the list by the length of the values.

def myFunc(n):
     return len (n)

x = ['a', 'b', 'c', 'd', 'e']
x.sort(key=myFunc)

print(x) 
python-arrays-method

Explanation – As it is shown clearly that it returned an array after arranging the elements according to length.

Example 10- Sort the list in descending order by the length of the values and reversed

def myFunc(n):
     return len (n)

x = ['a', 'def', 'bc', 'jklm', 'ghi']
x.sort(key=myFunc)

print(x) 
python-arrays-method

Explanation – As it is shown clearly that it returned an array after arranging the elements according to length.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

1 thought on “Quick Introduction To Python Arrays Method”

Leave a Comment

Stay in the loop

codingstreets