Array in Python | Set 2 (Important Functions)
Last Updated :
06 Sep, 2023
Array in Python | Set 1 (Introduction and Functions)
Array in Python | Set 2
Below are some more useful functions provided in Python for arrays:
Array Typecode Function
This function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data type of array initialization.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
# using typecode to print datatype of array
print ("The datatype of array is : ")
print (arr.typecode)
OutputThe datatype of array is :
i
Array itemsize Function
This function returns the size in bytes of a single array element. In this example, we are using itemsize function to find out the size in byte of an array element.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
# using itemsize to print itemsize of array
print ("The itemsize of array is : ")
print (arr.itemsize)
OutputThe itemsize of array is :
4
buffer_info() in Python
Returns a tuple representing the address in which array is stored and number of elements in it. In this example, we are using buffer_info() to do the same.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
# using buffer_info() to print buffer info. of array
print ("The buffer info. of array is : ")
print (arr.buffer_info())
OutputThe buffer info. of array is :
(140491260368688, 6)
count() in Python
Python count() function counts the number of occurrences of argument mentioned in array.
extend() in Python
This function appends a whole array mentioned in its arguments to the specified array. In this example, we are using extend() to append another array.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr1 = array.array('i',[1, 2, 3, 1, 2, 5])
arr2 = array.array('i',[1, 2, 3])
# using extend() to add array 2 elements to array 1
arr1.extend(arr2)
print ("The modified array is : ")
for i in range (0,9):
print (arr1[i], end=" ")
OutputThe modified array is :
1 2 3 1 2 5 1 2 3
Array fromlist() Function
This function is used to append a list mentioned in its argument to end of array. In this example, we are using fromlist() to append a list to end of array.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr = array.array('i',[1, 2, 3, 1, 2, 5])
li = [1, 2, 3]
# using fromlist() to append list at end of array
arr.fromlist(li)
# printing the modified array
print ("The modified array is : ",end="")
for i in range (0,9):
print (arr[i],end=" ")
OutputThe modified array is : 1 2 3 1 2 5 1 2 3
tolist() in Python
This function is used to transform an array into a list. In this example, we are using tolist() to convert an array to list.
Python3
# importing "array" for array operations
import array
# initializing array with array values
arr = array.array('i',[1, 2, 3, 1, 2, 5])
# using tolist() to convert array into list
li2 = arr.tolist()
# printing the new list
print ("The new list created is : ",end="")
for i in range (0,len(li2)):
print (li2[i],end=" ")
OutputThe new list created is : 1 2 3 1 2 5
Similar Reads
Operator Functions in Python | Set 2 Operator Functions in Python | Set 1 More functions are discussed in this article. 1. setitem(ob, pos, val) :- This function is used to assign the value at a particular position in the container. Operation - ob[pos] = val 2. delitem(ob, pos) :- This function is used to delete the value at a particul
5 min read
Array in Python | Set 1 (Introduction and Functions) Other than some generic containers like lists, Python in its definition can also handle containers with specified data types. The array can be handled in Python by a module named "array". They can be useful when we have to manipulate only specific data type values. Properties of ArraysEach array ele
7 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
How to pass an array to a function in Python Arrays in Python are implemented using the array module, which allows you to store elements of a specific type. You can pass an entire array to a function to perform various operations.Syntax to create an arrayarray(data_type, value_list)In this article, we'll explore how to pass arrays and lists to
3 min read
Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar
3 min read