Introduction to Python Array Functions
Array functions in python are defined as functions that will have arrays as parameters to the function and perform set of instructions to perform a particular task on input parameters to achieve a particular task is called array functions in python. Array functions will take an array as an argument, it will be implemented in such a way that it will operate on a different number of objects.
For example, we can define a function that will compute the average of an array of integers or float data types. Python by default takes arguments passed to the program will be stored in sys.argv[] which is an array of strings as the argument.
Methods of Python Array Functions
Given below are the different methods in python which will be used to perform different types of operations on array functions:
1. array(dataype, valuelist)
The above function, an array is used to create an array in python which accepts parameters as data type and value-list where data type is the type of the value-list like integer, decimal, float, etc. The value-list is a list of values of that particular data type.
Example:
Code:
a = array(‘i’,[1,2,3,4])
print(a)
The above example will create an array of ‘integer’ data type with values 1, 2, 3, 4 as its elements.
Output:
2. insert(pos, value)
The above function, insert() is used to insert an element to the array at a specific index or position. It will take pos, and value as its parameters where pos variables tell the position and value is the value need to insert into array.
Example:
Code:
a.insert(2, 9)
print(“Array after inserting 9 is”, a)
The above example will add a value 9 to the array ar at the index position 2 and the remaining elements will be arranged appropriately.
Output:
3. append(value)
The above function, append() is used to add value or append a value to the existing array in the end. It will take the value which we want to add as an argument.
Example:
Code:
a.append(6)
print(ar)
The above example will add a value 6 to the end of the array ar which is created in the previous example.
Output:
4. remove(value)
The above function, remove() will also remove the given element from the array. It will take value to remove as an argument and removes the element from the array and it won’t display the element.
Example:
Code:
a.remove(9)
print(a)
The above examples remove element 9 from the array ar and we will see list of all available elements in the array.
Output:
5. extend(arr)
The above function, extend() is used to add an array of values at the end of the existing array created. It will take an array of values or an array as its input argument.
Example:
Code:
a.extend([7,5,8])
print(a)
In the above example, we will add an array of values 7, 5, 8 to the end of the existing array ar.
Output:
6. pop()
The above function, pop() is used to remove an element from the array. It can take the argument as a position of the element we want to remove or by default it removes the element at the last index.
Example:
Code:
ar.pop()
It removes last element in the array and displays element.
Code:
ar.pop(4)
It removes the element at 4th index position in the array and displays element
Output:
7. index(value)
The above function, index() will return the first position of the value in the array, it will take the value as an argument to it and returns the first occurrence position of the value in the array as output.
Example:
Code:
ar.index(2)
The above example will return the position of the element 2 in the array ar as output.
Output:
8. reverse()
The above function, reverse() will reverse the array and returns the reversed array as an output of it. It doesn’t take any argument.
Example:
Code:
a.reverse()
print(“Array after reverse is:”,a)
The above example will reverse the array ar and stores the result in ar. The print statement will display the reversed array.
Output:
9. fromlist(list)
The above function, fromlist() is used to add a list to the end of the array. It will take a list as its input argument and the output of this function is an array with list appended at the end of the array.
Example:
Code:
li = [10,9,8]
a.fromlist(li)
print(“Array after appending the list is:”,a)
In the above example, we have created a list with a list of values of the same data type as an array and we called the function fromlist() to add the list to the array and the output is an array with list elements added at the end of the array.
Output:
10. count(value)
The above function, count() will tell the total number of occurrences of the given value in the array. It will take a value as an argument to it to know the total number of occurrences.
Example:
Code:
a.extend([2,1,3,2,1])
a.count(2)
In the above example, we have extended the array “a” with values 2, 1, 3, 2, 1 and we are trying to find the count of value 2 in the array after extending operation. It will give the number of occurrences of value 2 in the array.
Output:
Conclusion
Finally, its an overview of array functions in python. So far, we have seen what is an array function in python, how the array will be declared in python, different types of built-in array functions in python with a detail explanation with examples and its corresponding outputs.
Recommended Articles
This is a guide to Python Array Functions. Here we discuss the introduction to Python Array Functions and methods array functions respectively with examples. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses