Python has several built-in array methods that allow you to manipulate arrays efficiently.
Some of the commonly used array methods in Python include:
append() Python Method: This method adds an element to the end of an array.
extend() python method: This method adds elements from another array or iterable to the end of an array.
insert() python method : This method inserts an element at a specified position in an array.
remove() python method: This method removes the first occurrence of a specified element from an array.
pop() python method: This method removes and returns the last element of an array or the element at a specified position.
index() python method: This method returns the index of the first occurrence of a specified element in an array.
count() python method: This method returns the number of occurrences of a specified element in an array.
sort() python method : This method sorts the elements of an array in ascending or descending order.
reverse() python method: This method reverses the order of the elements in an array.
clear() python method: This method removes all elements from an array.
copy() python method: This method returns a shallow copy of an array. Any changes made to the copy will not affect the original array.
slice() python method: This method returns a slice of an array, which is a portion of the array specified by a start and end index.
len() pyhton method: This method returns the number of elements in an array.
any() python method: This method returns True if any element in the array is true. Otherwise, it returns False.
all() python method: This method returns True if all elements in the array are true. Otherwise, it returns False.
max() python method: This method returns the maximum element in an array.
min() python method: This method returns the minimum element in an array.
sum() python method: This method returns the sum of all elements in an array.
sorted() python method: This method returns a sorted version of the array. The original array is not modified.
zip() python method: This method takes one or more arrays and returns a list of tuples, where each tuple contains the corresponding elements from each array.
enumerate() python method: This method returns an iterator that yields pairs of (index, element) for each element in the array.
map() python method: This method applies a function to each element in the array and returns a new array with the results.
filter() python method: This method applies a function to each element in the array and returns a new array containing only the elements for which the function returned True.
reduce() python method: This method applies a function to the first two elements in the array, then applies the function to the result and the next element, and so on, until all elements have been processed. The final result is returned.
join() python method: This method concatenates the elements of an array into a single string, using a specified separator.
partition() python method: This method searches for a specified separator in an array and returns a tuple containing the part before the separator, the separator itself, and the part after the separator.
rpartition() python method: This method is similar to partition(), but it searches for the last occurrence of the separator.
split() python method: This method splits a string into an array of substrings, using a specified separator.
rsplit() python method: This method is similar to split(), but it splits the string starting from the end.
strip() python method: This method removes leading and trailing whitespace from each element in an array.
lstrip() python method: This method removes leading whitespace from each element in an array.
rstrip() python method: This method removes trailing whitespace from each element in an array.
capitalize() python method: This method capitalizes the first character of each element in an array.
title() python method: This method capitalizes the first character of each word in each element in an array.
upper() python method: This method converts each character in each element in an array to uppercase.
lower() python method: This method converts each character in each element in an array to lowercase.
swapcase() python method: This method swaps the case of each character in each element in an array.
replace() python method: This method replaces a specified substring with another substring in each element in an array.
isnumeric() python method: This method returns True if each character in each element in an array is a numeric character. Otherwise, it returns False.
isalpha() python method: This method returns True if each character in each element in an array is an alphabetic character. Otherwise, it returns False.
isalnum() python method: This method returns True if each character in each element in an array is an alphanumeric character. Otherwise, it returns False.
isspace() python method: This method returns True if each character in each element in an array is a whitespace character. Otherwise, it returns False.
startswith() python method: This method returns True if each element in an array starts with a specified substring. Otherwise, it returns False.
endswith() python method: This method returns True if each element in an array ends with a specified substring. Otherwise, it returns False.
These methods provide even more functionality for working with arrays in Python.Whether you need to search, insert, remove, or manipulate elements in an array, Python’s built-in array methods have got you covered.
Here are some examples of how to use each array method in Python and their respective use cases:
This method is used to add an element to the end of the array.
# Example 1 arr = [1, 2, 3] arr.append(4) print(arr) # Output: [1, 2, 3, 4] # Example 2 arr = [“apple”, “banana”, “cherry”] arr.append(“orange”) print(arr) # Output: [“apple”, “banana”, “cherry”, “orange”]
This method is used to add elements from an iterable object to the end of the array.
# Example 1 arr = [1, 2, 3] arr.extend([4, 5]) print(arr) # Output: [1, 2, 3, 4, 5] # Example 2 arr = ["apple", "banana", "cherry"] arr.extend(["orange", "grape"]) print(arr) # Output: ["apple", "banana", "cherry", "orange", "grape"]
This method is used to insert an element at a specified position in the array.
# Example 1 arr = [1, 2, 3] arr.insert(1, 4) print(arr) # Output: [1, 4, 2, 3] # Example 2 arr = ["apple", "banana", "cherry"] arr.insert(2, "orange") print(arr) # Output: ["apple", "banana", "orange", "cherry"]
This method is used to remove the first occurrence of an element from the array.
# Example 1 arr = [1, 2, 3, 4, 3] arr.remove(3) print(arr) # Output: [1, 2, 4, 3] # Example 2 arr = ["apple", "banana", "cherry", "banana"] arr.remove("banana") print(arr) # Output: ["apple", "cherry", "banana"]
This method is used to remove and return the last element from the array, or an element at a specified index.
# Example 1 arr = [1, 2, 3, 4] last_elem = arr.pop() print(last_elem) # Output: 4 print(arr) # Output: [1, 2, 3] # Example 2 arr = ["apple", "banana", "cherry"] second_elem = arr.pop(1) print(second_elem) # Output: "banana" print(arr) # Output: ["apple", "cherry"]
This method is used to get the index of the first occurrence of an element in the array.
# Example 1 arr = [1, 2, 3, 4, 3] index = arr.index(3) print(index) # Output: 2 # Example 2 arr = ["apple", "banana", "cherry", "banana"] index = arr.index("banana") print(index) # Output: 1
This method is used to count the number of occurrences of an element in the array.
# Example 1 arr = [1, 2, 3, 4, 3] count = arr.count(3) print(count) # Output:
This method is used to reverse the order of elements in the array.
# Example 1 arr = [1, 2, 3] arr.reverse() print(arr) # Output: [3, 2, 1] # Example 2 arr = ["apple", "banana", "cherry"] arr.reverse() print(arr) # Output: ["cherry", "banana", "apple"]
This method is used to sort the elements in the array in ascending or descending order.
# Example 1 arr = [3, 2, 1] arr.sort() print(arr) # Output: [1, 2, 3] # Example 2 arr = ["banana", "apple", "cherry"] arr.sort(reverse=True) print(arr) # Output: ["cherry", "banana", "apple"]
This method is used to create a shallow copy of the array.
# Example 1 arr1 = [1, 2, 3] arr2 = arr1.copy() print(arr2) # Output: [1, 2, 3] # Example 2 arr1 = ["apple", "banana", "cherry"] arr2 = arr1.copy() print(arr2) # Output: ["apple", "banana", "cherry"]
This method is used to remove all elements from the array.
# Example 1 arr = [1, 2, 3] arr.clear() print(arr) # Output: [] # Example 2 arr = ["apple", "banana", "cherry"] arr.clear() print(arr) # Output: []
This function is used to get the length of the array.
# Example 1 arr = [1, 2, 3] length = len(arr) print(length) # Output: 3 # Example 2 arr = ["apple", "banana", "cherry"] length = len(arr) print(length) # Output: 3
Quiz about Array methods in python
1-Which method is used to append an element to the end of an array in Python?
A. append() B. insert() C. extend() D. pop()
Answer: A
2-Which method is used to insert an element at a specified position in an array in Python?
A. append() B. insert() C. extend() D. pop()
Answer: B
3-Which method is used to append multiple elements to an array in Python?
A. append() B. insert() C. extend() D. pop()
Answer: C
4-Which method is used to remove and return the last element from an array in Python?
A. append() B. insert() C. extend() D. pop()
Answer: D
5-Which method is used to remove the first occurrence of a specified element from an array in Python?
A. remove() B. pop() C. clear() D. index()
Answer: A
6-Which method is used to return the index of the first occurrence of a specified element in an array in Python?
A. remove() B. pop() C. clear() D. index()
Answer: D
7-Which method is used to reverse the order of elements in an array in Python?
A. reverse() B. sort() C. clear() D. copy()
Answer: A
8-Which method is used to sort the elements of an array in ascending or descending order in Python?
A. reverse() B. sort() C. clear() D. copy()
Answer: B
9-Which method is used to remove all the elements from an array in Python?
A. append() B. insert() C. extend() D. clear()
Answer: D
10-Which method is used to create a copy of an array in Python?
A. copy() B. reverse() C. sort() D. clear()
Answer: A
11-Which method is used to return the length of an array in Python?
A. size() B. length() C. count() D. len()
Answer: D
12-Which method is used to check if a specified element is present in an array in Python?
A. in_array() B. contains() C. includes() D. contains()
Answer: D
13-Which method is used to return a new array with the elements of the original array that pass a certain condition in Python?
A. map() B. filter() C. reduce() D. apply()
Answer: B
14-Which method is used to return a new array with the elements of the original array transformed by a function in Python?
A. map() B. filter() C. reduce() D. apply()
Answer: A
15-Which method is used to concatenate two or more arrays in Python?
A. concat() B. join() C. merge() D. append()
Answer: A
16-Which method is used to return the highest value in an array in Python?
A. max() B. min() C. high() D. top()
Answer: A
17-Which method is used to return the lowest value in an array in Python?
A. max() B. min() C. low() D. bottom()
Answer: B
18-Which method is used to remove the element at a specified position from an array in Python?
A. remove() B. pop() C. delete() D. extract()
Answer: B
19-Which method is used to return a new array with the elements of the original array in a random order in Python?
A. shuffle() B. randomize() C. scramble() D. mix()
Answer: A
20-Which method is used to return the sum of all the elements in an array in Python?
A. sum() B. total() C. add() D. accumulate()
Answer: A
21-.Which method is used to check if all the elements in an array are the same in Python?
A. same() B. equals() C. compare() D. all()
Answer: D
22.Which method is used to return a new array with the unique elements of the original array in Python?
A. unique() B. distinct() C. deduplicate() D. remove_duplicates()
Answer: B
23.Which method is used to return the frequency of a specified element in an array in Python?
A. count() B. frequency() C. occurrences() D. total()
Answer: A
24.Which method is used to return a new array with the elements of the original array sorted in reverse order in Python?
A. reverse() B. sort() C. inverted() D. descending()
Answer: A
25.Which method is used to return the index of the last occurrence of a specified element in an array in Python?
A. index() B. last_index() C. find_last() D. rindex()
Answer: D
26.Which method is used to return a new array with the elements of the original array repeated a certain number of times in Python?
A. repeat() B. replicate() C. duplicate() D. copy()
Answer: A
27.Which method is used to return a new array with the elements of the original array sorted based on a key function in Python?
A. sort() B. key_sort() C. order_by() D. sorted()
Answer: D
28.Which method is used to return the product of all the elements in an array in Python?
A. prod() B. product() C. multiply() D. accumulate()
Answer: A
29.Which method is used to return a new array with the elements of the original array sorted in ascending order based on a key function in Python?
A. sort() B. key_sort() C. order_by() D. sorted()
Answer: D
30.Which method is used to remove all the occurrences of a specified element from an array in Python?
A. remove_all() B. delete_all() C. pop_all() D. filter()
Answer: D